顯示此錯誤的最小工作示例:from os import listdir, getcwdfrom os.path import isfile, join, realpath, dirnameimport csvdef gd(mypath, myfile): # Obtain the number of columns in the data file with open(myfile) as f: reader = csv.reader(f, delimiter=' ', skipinitialspace=True) for i in range(20): row_20 = next(reader) # Save number of clumns in 'num_cols'. num_cols = len(row_20) return num_colsmypath = realpath(join(getcwd(), dirname(__file__)))# Iterate through all files. Stores name of file in 'myfile'.for myfile in listdir(mypath): if isfile(join(mypath,myfile)) and (myfile.endswith('.dat')): num_cols = gd(mypath, myfile)print(num_cols)我在該文件夾中只有一個名為“ data.dat”的文件,并python返回錯誤:----> 9 with open(myfile) as f:....IOError: [Errno 2] No existe el archivo o el directorio: u'data.dat'轉(zhuǎn)換為無文件或目錄:u'data.dat'。為什么在文件名的開頭添加u,如何獲取正確解析文件名的代碼?
添加回答
舉報
0/150
提交
取消