我想知道是否可以使用返回raw_input()
来创建文件名?
到目前为止我所拥有的:
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
ttnum=str(raw_input('Please enter ticket number: '))
ttnum
filename=askopenfilename()
abspath = os.path.abspath(filename)
dname = os.path.dirname(abspath)
os.chdir(dname)
f=open(filename)
contents=f.read()
file_len(filename)
file_scan(filename)
调用 ttnum 的部分代码:
def file_len(filename):
#Count the number of line in the Text File
f1 = open(("WiFi Logs report for tt " + ttnum,'w'))
with open(filename) as f:
for i, l in enumerate(f):
pass
f1.write('Total number of lines in file: ' + str(i+1) + '\n' + '\n')
f1.close()
def file_scan(filename):
#List of issues to Scan For
f1 = open(("WiFi Logs report for tt " + ttnum,'a'))
我可以输入输入没有问题(在本例中为 12345),但是一旦输入代码,我会得到以下信息:
Traceback (most recent call last):
File "M:\WiFi Log Scanner\WiFi_Log_Scanner.py", line 153, in <module>
file_len(filename)
File "M:\WiFi Log Scanner\WiFi_Log_Scanner.py", line 4, in file_len
f1 = open(("WiFi Logs report for tt " + ttnum,'w'))
TypeError: coercing to Unicode: need string or buffer, tuple found
我认为一开始的 str() 会确保它是一个字符串而不是一个元组?
任何见解将不胜感激。
谢谢,
乔