![](/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.