0

I'm trying to rename the file names and extensions of all the files in a directory and move them to a new directory. I've read multiple post on how to do it but for some reason I haven't been successful and I've been stuck on this for 3 days now and feel like I'm doing something careless. Somebody get me on track please. This is the latest way I've been trying.

import os

previousName = 'Macintosh HD⁩/⁨Users⁩/⁨kunductor/⁨Desktop⁩/⁨folder3/windeffect.asd'

newName = 'Macintosh HD⁩/Users⁩/kunductor⁩/Desktop⁩/folder4/wind.wav'

os.rename(previousName,newName)

When I run the code above I get the message:

Traceback (most recent call last):
  File "rename.py", line 7, in <module>
    os.rename(previousName,newName)
FileNotFoundError: [Errno 2] No such file or directory

If it matters, I'm using macOS Mojave, version 10.14.2.

Sreekiran A R
  • 3,123
  • 2
  • 20
  • 41
  • [Looks like](https://docs.python.org/3/library/os.html#os.rename) you'd need to use the `src_dir_fd` and `dst_dir_fd` parameters to specify the path for the file you're renaming, if I'm understanding those correctly. ...Actually, [this](https://stackoverflow.com/a/8858026/2617068) says what you're doing should work. What doesn't work when you try that? – TigerhawkT3 Feb 03 '19 at 02:09
  • You might want to construct the file names with `os.path.join()`. See https://docs.python.org/3/library/os.path.html#os.path.join – Red Cricket Feb 03 '19 at 03:45
  • 3
    `Macintosh HD` is almost certainly not supposed to be the first path component. `/Users/kunductor/...` seems more likely to be correct. – user2357112 Feb 03 '19 at 05:55
  • i tried that too. thats how i was doing it the first couple of days. i dont know why its not working but it looks like im going to have to just spend a day and a half doing this manually until my light bulb moment. – TheKunductor Feb 03 '19 at 06:01
  • @TheKunductor quick sanity check to see if the path is correct, just do `os.path.exists(my_path)` and if it's False, you've not got the right path. – Athena Feb 03 '19 at 06:25

2 Answers2

0

I tried replicating the same using Python 3 on Mojave 10.14.2. Use the paths starting from '/Users', and don't include Macintosh HD. The code runs perfectly when both folder3 and folder4 exist. I got a similar error when folder4 was removed, and the error message also specified the paths I passed as parameters.

If that's what you're experiencing, ensure that the directory you're trying to move the file to exists before calling os.rename. This can be done in Python itself by using the os.mkdir method. Since it throws an error if the directory already exists, you can check that by using the os.path.exists method.

shriakhilc
  • 2,922
  • 2
  • 12
  • 17
0

this is the code that worked. I think the problem was that i was trying to change a non-audio file to a .wav and the system was rejecting it.

import os 

# Function to rename multiple files 
def main(): 
    i = 0

    for filename in os.listdir('/Users/vfloyd/Desktop/uu/'): 
        dst ="Kick" + str(i) + ".wav"
        src = '/Users/vfloyd/Desktop/uu/'+ filename 
        dst ='/Users/vfloyd/Desktop/newD/'+ dst 

        # rename() function will 
        # rename all the files 
        os.rename(src, dst) 
        i += 1

# Driver Code 
if __name__ == '__main__': 

    # Calling main() function 
    main()