Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/list/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 在Python中将文件名写入数组_Arrays_List_Append - Fatal编程技术网

Arrays 在Python中将文件名写入数组

Arrays 在Python中将文件名写入数组,arrays,list,append,Arrays,List,Append,我使用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 ="(

我使用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=[];
使用

我收到以下错误:“SyntaxError:无法分配给函数调用”

如果我只是尝试使用indes(即filelist[I]=newfilename)将文件添加到filelst数组中,则会得到一个索引超出范围的错误


如何“动态”创建重命名文件名列表?谢谢。

好的..所以我终于明白append想要“将“东西”附加到列表中,而不是像我最初尝试的那样“将索引I处的东西附加到列表中”

因此,正确使用append的方法是:

filelist.append(newfilename)
filelist.append(newfilename)