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'不知道文件的位置,它只是一個字符串。 –
這很有道理!非常感謝!! –