Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
rename files in a folder
#1
Hi all,
I'm trying to rename about 25 files in a directory. If I have a directory that has a bunch of .jpg's that have the start out, "scanXXX", I'd like to change the name to "356EXXX".

Here is what I've researched from the lovely internets,
###rename files 0601
import os, sys
###
def main():

    for count, filename in enumerate(os.listdir("C:\PG2")):
        dst ="356E" + str(count) + ".jpg"
        src ='PG2' + filename
        dst ='PG2' + dst

        os.rename(src, dst)

if __name__ == '__main__':

    main()
Here is my error message:
Error:
===================================== RESTART: C:/PG2/rename.py ===================================== Traceback (most recent call last): File "C:/PG2/rename.py", line 15, in <module> main() File "C:/PG2/rename.py", line 11, in main os.rename(src, dst) FileNotFoundError: [WinError 2] The system cannot find the file specified: 'PG2356E2-26.jpg' -> 'PG2356E0.jpg' >>>
Could this be a permissions issue? I've noticed on WIN10 that if you're not elevated, half the time scripts ain't workin'.

Thanks for any help,
windows10 home, hp pavilion
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply
#2
Take a close look at the filename error:

Error:
The system cannot find the file specified: 'PG2356E2-26.jpg'
You are intending to operate on a path name (like "C:\PG2\356E2-26.jpg"), but are instead handing to rename() a string with all that mushed together. You didn't put any path separators in. You could do that manually, but better is to use os.path functions to form them.

dir = "C:\PG2"
for count, filename in enumerate(os.listdir(dir)):
    dst = os.path.join(dir, f"356E{count}.jpg")
    src = os.path.join(dir, filename)

    os.rename(src, dst)
[...]
Reply
#3
@bowlofred,
Many thanks. That did the trick.

I don't use os.path that much. I will start to incorporate.

Thanks again.
"Human history becomes more and more a race between education and catastrophe." - H. G. Wells (1866-1946)
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Compare folder A and subfolder B and display files that are in folder A but not in su Melcu54 3 466 Jan-05-2024, 05:16 PM
Last Post: Pedroski55
  Rename files in a folder named using windows explorer hitoxman 3 693 Aug-02-2023, 04:08 PM
Last Post: deanhystad
  Rename all files in a folder hitoxman 9 1,387 Jun-30-2023, 12:19 AM
Last Post: Pedroski55
  How to loop through all excel files and sheets in folder jadelola 1 4,333 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  python gzip all files from a folder mg24 3 3,814 Oct-28-2022, 03:59 PM
Last Post: mg24
  delete all files and subdirectory from a main folder mg24 7 1,530 Oct-28-2022, 07:55 AM
Last Post: ibreeden
  Merge all json files in folder after filtering deneme2 10 2,256 Sep-18-2022, 10:32 AM
Last Post: deneme2
  Rename part of filename in multiple files atomxkai 7 7,217 Feb-18-2022, 10:03 PM
Last Post: atomxkai
  Compare filename with folder name and copy matching files into a particular folder shantanu97 2 4,392 Dec-18-2021, 09:32 PM
Last Post: Larz60+
  Rename Files based on XML file klturi421 3 2,136 Oct-22-2021, 07:37 PM
Last Post: klturi421

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020