import os
def soldier(ppth):
pth= os.listdir(ppth)
for filename in pth:
os.rename(filename,filename.lower())
print(pth)
soldier("D://QwertyA")
Asked
Active
Viewed 636 times
0
-
Even if i use os.rename(filename,filename.capitalize()). Its generating an error FileNotFoundError: [WinError 2] The system cannot find the file specified: 'asd.docx' -> 'Asd.docx' – BABA ADAM May 14 '21 at 21:13
-
You probably need to use the full path to the file `f"{ppth}/{filename}"` – cam May 14 '21 at 21:22
2 Answers
1
You can use the capitalize method.
string.capitalize()
def soldier(ppth):
pth= os.listdir(ppth)
for filename in pth:
os.rename(f"{ppth}/{filename}",f"{ppth}/{filename.lower().capitalize()}")

cam
- 778
- 6
- 9
-
No!! i want to capitalize the first letters of the file present in the specific directory – BABA ADAM May 14 '21 at 21:08
-
Right. ```import os def soldier(ppth): pth= os.listdir(ppth) for filename in pth: os.rename(filename,filename.lower().capitalize()) print(pth) soldier("D://QwertyA") ``` – cam May 14 '21 at 21:13
-
0
You can try something like this.
import os
fileName = 'test.txt'
newFileName = fileName.capitalize()
print(newFileName)
os.rename(f'/path/to/file/{fileName}',f'/path/to/file{newFileName}')
Seems like my file has been capitalized.

Buddy Bob
- 5,829
- 1
- 13
- 44