pythonos.path

Python reporting abspath as directly within CWD rather than the actual correct directory


I am attempting to code a small script to sort music tracks for me. I'm running into a problem where the script looks for the file in the wrong place.

The user defines the directory to look for files in via input, and validates the directory exists. It then filters out anything that isn't an mp3 file. It reports the number of unsorted files found correctly. This is all working fine.

My .py file is located in Desktop\Programming\Music The directory with files I'm attempting to sort is located in Desktop\Programming\Music\Test_Music

The script is reporting the absolute path of the file as directly within my CWD, rather than within the real directory Test_Music.

INPUT:

.
.
.

#list all files in parent directory
tracks = os.listdir(parent_dir)

#Filtering only the files, not folders.
tracks = [f for f in tracks if os.path.isfile(parent_dir+'/'+f)] 

#report how many tracks were found
print("Found " + str(len(tracks)) + " unsorted files in parent directory.")

#troubleshoot, report the current working directory
print("CWD: " + str(Path.cwd()))

#troubleshoot, print the absolute path of each track
for track in tracks:
    #convert track string to a windowspath object type
    p_track = Path(track)

    #print the absolute path of each track
    print(os.path.abspath(p_track))

OUTPUT:

Found 11 unsorted files in parent directory.
CWD: C:\Users\im\Desktop\Programming
C:\Users\im\Desktop\Programming\01 Sanctuary 1.mp3
C:\Users\im\Desktop\Programming\01 Soft.mp3
C:\Users\im\Desktop\Programming\01 Something Bigger, Something Brigh.mp3
C:\Users\im\Desktop\Programming\01 Something New 1.mp3
C:\Users\im\Desktop\Programming\01 Something.mp3
C:\Users\im\Desktop\Programming\01 Somewhere in October.mp3
C:\Users\im\Desktop\Programming\01 Stars (feat. Eva Maria).mp3
C:\Users\im\Desktop\Programming\01 Stav.mp3
C:\Users\im\Desktop\Programming\01 Stay 1.mp3
C:\Users\im\Desktop\Programming\01 Stupid Love.mp3
C:\Users\im\Desktop\Programming\01 Sugar Haze 2.mp3

This is incorrect. All tracks are located in Desktop\Programming\Music\Test_Music. So where did I go wrong?

I've tried defining the path as, for example, (os.path.dirname(str(track))) + (os.path.basename(str(track))) but (os.path.dirname(str(track)) shows up completely empty, as does os.path.dirname(Path(track)). I'm new to coding so I'm likely missing something obvious.


Solution

  • The problem is here:

    tracks = [f for f in tracks if os.path.isfile(parent_dir+'/'+f)] 
    

    You check if parent_dir+'/'+f is a file and then put f in the list. A simple fix should be

    tracks = [parent_dir+'/'+f for f in tracks if os.path.isfile(parent_dir+'/'+f)] 
    

    Personally, I prefer to use Path from pathlib (which you use in some places as well).

    Then you could do something like:

    parent_path = Path(parent_dir).absolute()
    tracks = [entry for entry in parent_path.iterdir() if entry.is_file()]