![](/img/trans.png)
[英]Windows/Python Error WindowsError: [Error 3] The system cannot find the path specified
[英]The system cannot find the file specified windows error python
我想將測試文件夾中的所有文件重命名為1、2、3,依此類推
import os, sys, path
path = r"F:\test"
dirs = os.listdir(path)
print(dirs)
count = 1
for files in dirs:
str1 = str(count)
os.rename(files, str1)
count += 1
但是我的代碼給了我這個錯誤:WindowsError:[錯誤2]系統找不到指定的文件
dirs
是路徑的列表,對其進行迭代將不會為您提供目錄的內容。 您將需要另一個os.listdir
。
另外,要重命名文件,您必須仔細檢查每個文件。
更好的解決方案是:
import os
count = 1
path = r"F:\test"
for root, dirs, files in os.walk(path):
for filename in files:
os.rename(os.path.join(root, filename), os.path.join(root, str(count)))
count += 1
只需添加一行即可更改當前工作目錄。
import os, sys, path
path = r"F:\test"
dirs = os.listdir(path)
os.chdir(path) # Change the current working directory
print(dirs)
count = 1
for files in dirs:
str1 = str(count)
os.rename(files, str1)
count += 1
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.