這是我得到的錯誤:系統找不到指定的文件:'1.jpg' -> '0.jpg'即使我在目錄中有一個名為 1.jpg 的文件。我正在制作文件重命名腳本,該腳本重命名給定的目錄中的所有文件,該數字隨每個文件增加 +1。import osdef moving_script():directory = input("Give the directory")xlist = os.listdir(directory)counter = 0for files in xlist: os.rename(files, str(counter)+".jpg") counter = counter + 1moving_script()它應該將所有文件重命名為“0.jpg”、“1.jpg”等
2 回答
搖曳的薔薇
TA貢獻1793條經驗 獲得超6個贊
代碼:
import os
def moving_script():
directory = input("Give the directory")
xlist = os.listdir(directory)
counter = 0
for files in xlist:
os.rename(os.path.join(directory, files),
os.path.join(directory, str(counter)+".jpg"))
counter = counter + 1
if __name__ == '__main__':
moving_script()
結果:
~/Documents$ touch file0 file1 file2 file3 file4
ls ~/Documents/
file0 file1 file2 file3 file4
$ python renamer.py
Give the directory'/home/suser/Documents'
$ ls ~/Documents/
0.jpg 1.jpg 2.jpg 3.jpg 4.jpg
添加回答
舉報
0/150
提交
取消
