0

I want to rename multiple files in a directory, if they start with "#", for example:

#222_message_split -> 222_message_split

#013_message_split2 -> 013_message_split2

edit: I tried this:

for filename in os.listdir(PATH):
    if filename.startswith("#"):
        os.rename(filename, filename[1:])

edit2: with the help of pstatix I got the code to work, now checking subdirectories for "#_____" files as well.

for root, dirs, files in os.walk(PATH):
for dir in dirs:
    if dir.startswith("#"):
        org_fp = os.path.join(root, dir)
        new_fp = os.path.join(root, dir[1:])
        os.rename(org_fp, new_fp)
nEWBOB
  • 55
  • 9

1 Answers1

1

You were close, although your indentation was off:

Original Question:

for filename in os.listdir(PATH):
    if filename.startswith("#"):
        org_fp = os.path.join(PATH, filename)
        new_fp = os.path.join(PATH, filename[1:])
        os.rename(org_fp, new_fp)

os.listdir() doesn't return the full path even if PATH was a full path; only basenames are listed. So you must provide a full path for os.rename() to function correctly; done using os.path.join().

Comment Requested Update:

for root, dirs, files in os.walk(PATH):
    for file in files:
        if file.startswith("#"):
            org_fp = os.path.join(root, file)
            new_fp = os.path.join(root, file[1:])
            os.rename(org_fp, new_fp)

Take a look at the docs and this post for information.

pstatix
  • 3,611
  • 4
  • 18
  • 40
  • that works perfect thank you a lot pstatix! unfortunately I have to bother you even more, because I would like my code to traverse every subfolder as well, what do I have to add to my code to achieve this? – nEWBOB Jan 09 '18 at 15:15
  • again, thanks a lot, I will try this updated code and check the docs ect., glad that you helped me! – nEWBOB Jan 09 '18 at 15:35
  • @DanielSeidl You selected as the answer to your question; did you verify it worked? – pstatix Jan 09 '18 at 15:41
  • did that before reading the update, since it is the answer to the initial question I asked. Until now I can't get it to work, but I am still trying to get into the docs and the post you linked, since I am coding for about a month now only. Will try to understand what you did, and what "walk()" really does :) basically I have a directory containing folders, which are containing subfolders and so on, in some there are files/folders starting with the # -> the thing I want removed. – nEWBOB Jan 09 '18 at 15:48
  • @DanielSeidl `os.walk()` returns a tuple of 3 items. The docs describes what it does and provides an example. Should have everything you need. When you get it working or have more questions, come back and ask or select as answer. – pstatix Jan 09 '18 at 15:49
  • I got it to work now, had to change for file in files to for dir in dirs, and change the file to dir in both the org_fp and the new_fp. Thank you for your fast answers :) – nEWBOB Jan 09 '18 at 16:23
  • @DanielSeidl Not sure I follow, can you post the updated block with the changes you just mentioned in your original post? – pstatix Jan 09 '18 at 16:26
  • I edited the original post, now showing the solution to my problem. – nEWBOB Jan 09 '18 at 16:31
  • @nEWBOB (name change?). So the items you wanted to rename were directories and not files? – pstatix Jan 09 '18 at 16:33
  • yes name change, was a bit to fast when registering I guess. Thats correct, after solving the problem I realised my question was a bit misleading, hence the "for file in files" is not wrong, but an initial mistake from my side, since I really meant directories. – nEWBOB Jan 09 '18 at 16:55