我使用glob读取目录中的文件,然后使用os.rename将每个文件重命名为更易读的名称。
for file_name in glob.glob(path+'*.txt'):
newfilename = 'run'+str(i)+'.csv' # rename filenames to something more readable
os.rename(file_name,path + newfilename) #put r before path if error ="(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape"
当我尝试将创建的每个新文件写入到一个数组(列表)中时,该数组已初始化为:
filelist=[];
使用
filelist.append(i)=newfilename
我得到以下错误:"SyntaxError: cannot assign to function call“
如果我只是尝试使用indeces将文件添加到filelst数组中,即filelisti=newfilename,那么我就会得到一个索引超出范围的错误。
如何“即时”创建此重命名文件名列表?谢谢。
发布于 2020-11-24 01:19:31
Ok..so我终于明白append想要“将”事物“追加”到列表中,而不是像我最初试图做的那样,“将索引i处的事物追加到列表中”。
因此,使用append的正确方法是:
filelist.append(newfilename)
https://stackoverflow.com/questions/64970901
复制相似问题