显示此错误的最小工作示例:
from os import listdir, getcwd
from os.path import isfile, join, realpath, dirname
import csv
def 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_cols
mypath = 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'
转换为无文件或目录:u'data.dat'。
为什么在文件名的开头添加u,如何获取正确解析文件名的代码?
守着星空守着你
相关分类