How do I create a file path on a Mac?

Question:

I’m working an OS X platform with Python 3 and I’m not quite sure how to create file paths that link into directories. I know that on the Windows platform it would look something like

import os
path = 'C:\Users\User\Desktop\<directory name>'
os.mkdir(path)
file = open(path + '\<file name>.txt', w)
Asked By: Jason

||

Answers:

Paths on OSX use the forward slash (which you can also use on windows). Other than that, the only difference is that OSX (and most other non-windows OS’s) don’t have drive letters.

The root of any OSX system is “/”, everything else is below that. So, for example, your home directory is likely “/Users/myusername”. OSX paths are case-insensitive, but case-preserving. That means that “/users/myusername” and “/Users/myusername” go to the same place. If the directory is created with capital letters, the capital letters is what will show with ls, in the finder, etc.

os.mkdir works the same way on all platforms, assuming you give a valid path.

Answered By: Bryan Oakley

Similar issue but the path always creates outside of the folder instead of inside the folder.

import os, shutil
path = r"/Users/temi.brown/Library/CloudStorage/OneDrive-Personal/Python Tutorials/"
folder_name = [" image files" , " doc files" , " web files"]

for loop in range(0,3):
    if not os.path.exists(path + folder_name[loop]):
        os.makedirs((path + folder_name[loop]))

I want it to make these 3 folders INSIDE python tutorials but it makes the path inside one drive-personal. Can anyone please help with my code. thanks

Answered By: Temitope Brown
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.