0
import os
path="/home/jayabalan/Desktop/testdocker"
for i in os.listdir(path):
os.rename(i,i.upper())
运行在CentOS 7上面的代码蟒引发错误如下:文件名的情况下改变投Os.error [errorno 2]在CentOS 7
Traceback (most recent call last):
File "casechange.py", line 4, in <module>
os.rename(i,i.upper())
OSError: [Errno 2] No such file or directory
但是,当我试图通过打印文件添加“打印(i)”在文件夹“testdocker”内正确列出文件
在windows中运行此casechange程序时发生同样的问题。
"C:\Program Files\Python\python.exe" "G:/pycharm projects/jaitestpractice/practicefile.py"
Traceback (most recent call last):
File "G:/pycharm projects/jaitestpractice/practicefile.py", line 3, in <module>
os.rename(i, i.replace(" ", "_").upper())
FileNotFoundError: [WinError 2] The system cannot find the file specified: 'jfas.txt' -> 'JFAS.TXT'
但打印如何listdir同时该文件正确的说,目录时,我给打印以下(I)语句,循环? –
它打印文件名,但不打印包含路径的文件名。 'os.rename'不知道文件的位置,它只是一个字符串。 –
这很有道理!非常感谢!! –