文件名大小写更改会在CentOs 7上抛出Os.error[errorno 2]python

文件名大小写更改会在CentOs 7上抛出Os.error[errorno 2]python,python,linux,python-3.x,centos7,Python,Linux,Python 3.x,Centos7,在CentOS 7上运行上述代码会引发如下错误: import os path="/home/jayabalan/Desktop/testdocker" for i in os.listdir(path): os.rename(i,i.upper()) 回溯(最近一次呼叫最后一次): 文件“casechange.py”,第4行,在 重命名(i,i.upper()) OSError:[Errno 2]没有这样的文件或目录 但是当我尝试通过添加“print(I)”来打印文件时,会

在CentOS 7上运行上述代码会引发如下错误:

import os
path="/home/jayabalan/Desktop/testdocker"
for i in os.listdir(path):
        os.rename(i,i.upper())
回溯(最近一次呼叫最后一次):
文件“casechange.py”,第4行,在
重命名(i,i.upper())
OSError:[Errno 2]没有这样的文件或目录
但是当我尝试通过添加“print(I)”来打印文件时,会在文件夹“testdocker”中正确列出文件

在windows中运行此casechange程序时也会出现同样的问题

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
“C:\Program Files\Python\Python.exe”“G:/pycharm projects/jaitestpractice/practicefile.py”
回溯(最近一次呼叫最后一次):
文件“G:/pycharm projects/jaitestpractice/practicefile.py”,第3行,在
重命名(i,i.replace(“,“”).upper())
FileNotFoundError:[WinError 2]系统找不到指定的文件:“jfas.txt”->“jfas.txt”

您只需在文件路径前面加上前缀

"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
只返回文件名,并在当前目录中查找文件

"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'
因此:


但是,当我在for循环下面给出print(I)语句时,listdir如何正确地打印上述目录中的文件呢?它打印的是文件名,而不是带有路径的文件名
os.rename
不知道文件在哪里,它只有一个字符串。这很有意义!谢谢!!
import os
path="/home/jayabalan/Desktop/testdocker"
for i in os.listdir(path):
        os.rename(os.path.join(path, i), i.upper())