我搜索并编写了这样的代码,但是没有用。请给我建议。 我已经下载了许多媒体文件。我想在其中添加序列号。 该代码不起作用,但甚至不会注意到错误。 请帮助我
import os
path = os.getcwd()
dirc = os.listdir(path)
n = 0
for file in dirc:
os.chdir(path)
if file.endswith('*.txt'):
rename(srt(n) + file, file)
n += 1
答案 0 :(得分:0)
import os
path = os.getcwd()
n = 1
for filename in os.listdir(path):
if filename.endswith('.txt'):
src = path + '\\' + filename
dst = path + '\\' + str(n) + '-' + filename
os.rename(src, dst)
n+=1
以上代码将文件名abcd.txt
替换为1-abcd.txt
,依此类推。