Finding a file's directory address on a Mac - python

I am working with a Macbook programming python. What I want to know is how I can access certain files using Python's file functions. A google search failed me.
For example, Windows would be something like this:
f = open(r'C:\text\somefile.txt')
How would I access something from a folder saved on the Desktop of a Mac?

The desktop is just a subdirectory of the user’s home directory. Because the latter is not fixed, use something like os.path.expanduser to keep the code generic. For example, to read a file called somefile.txt that resides on the desktop, use
import os
f = open(os.path.expanduser("~/Desktop/somefile.txt"))
If you want this to be portable across operating systems, you have to find out where the desktop directory is located on each system separately.

f = open (r"/Users/USERNAME/Desktop/somedir/somefile.txt")
or even better
import os
f = open (os.path.expanduser("~/Desktop/somedir/somefile.txt"))
Because on bash (the default shell on Mac Os X) ~/ represents the user's home directory.

You're working on a Mac so paths like "a/b/c.text" are fine, but if you use Windows in the future, you'll have to change all the '/' to '\'. If you want to be more portable and platform-agnostic from the beginning, you better use the os.path.join operator:
import os
desktop = os.path.join(os.path.expanduser("~"), "Desktop")
filePath = os.path.join(desktop, "somefile.txt")
f = open(filePath)

If this is still an issue, I had the same problem and called Apple. I learned that the file I'd created was saved on iCloud. The Apple guy told me to save the file locally. I did that and the problem was solved.

Related

Is this good way to get path to desktop?

just want to ask if this code in python is valid to use on any OS, i mean, i want to use the path then for exporting txt file to desktop so for example if user on Mac will not have problem, or is there another way to do it?
def get_path():
PATH = os.path.normpath(os.path.expanduser('~/Desktop'))
return PATH
On windows you can easily get Desktop path by joining with the environment variable of user. Here is an example:
import os
desktop_path = os.path.join(os.path.join(os.environ['USERPROFILE']), 'Desktop')
This should work for most users, Windows as well as Linux and MacOS have their desktop (typically) located under ~/Desktop. Do note however this location can be changed by the user so there is no guarantuee.
Windows: Usually ~/Desktop but can be changed, see https://superuser.com/q/1663587/138216
Linux: Typically ~/Desktop but could be something else, see https://unix.stackexchange.com/questions/391915/where-is-the-path-to-the-current-users-desktop-directory-stored
Mac: Also typcially ~/Desktop http://etutorials.org/Mac+OS/mac+os+x+power+tools/Part+II+Files+Finders+Docks+and+Apps+Including+Classic/Chapter+5+Finagle+Files+and+Foil+Finder+Frustration/The+Desktop+and+the+Desktop+Folder/#:~:text=In%20Mac%20OS%20X%2C%20because,these%20folders%20in%20Chapter%201).

PyInstaller & NSIS Zip2Exe - Referencing files outside of your PC

I created an application that references multiple shapefiles. Example below:
df = gpd.GeoDataFrame.from_file(r'C:\PATH_ON_OWN_PC\FILE_NAME.shp')
As you can see from the above, the program references files from my own PC.
I then used NSIS to create a setup.exe file so the program can be downloaded and used with other PCs. I included all the datasets in the zip file.
The issue is when the application runs the script it's still referencing the path on my own PC, which obviously won't work.
Is there a way to alter the code so it reads the files from the downloadable .exe file, so it reads the files no matter the PC it's downloaded from.
Thanks!
You should be able to use something like this so that your .exe will work on other computers. Here is a generic example that points to a file inside of my Scripts dir, where my python.exe is located, for one of my Conda envs. You can replace the second and third params in os.path.join() to match up with the path of your shapefiles.
import os
import sys
python_exe_dir = os.path.dirname(sys.executable)
sample_file_path = os.path.join(python_exe_dir, 'Scripts', 'gdal2tiles.py')
print(sample_file_path)
'C:\\Users\\matth\\anaconda3\\envs\\gpd_0\\Scripts\\gdal2tiles.py'
So in your case, you'd end up with something like this.
df = gpd.GeoDataFrame.from_file(sample_file_path)

Any universal keyword to be use in place of user's name folder in directory in Windows

I'm trying to make a video downloader python program on windows. In my computer, the download folder is in C:\Users\Suraj\Downloads. If someone else uses my code in their computer, the download folder is in C:\Users*XYZ\Downloads. How to solve this directory problem(Suraj, XYZ)?
For me it looks that you are looking for os.path.expanduser, which can be used following way:
import os
homepath = os.path.expanduser('~')
downloadspath = os.path.expanduser(os.path.join('~','Downloads'))
according to docs it should work in Linux as well Windows, but I did not test second, so please test it before use.

How to find a folder path in mac os x using python?

I am new to Mac OS X (10.10.4). I want to write a code by using python 2.7, which first I have to check if a folder "Pictures" exists, and follow by saving a file into it. However, I dunno the folder path.
folder = "~/usr/my_name/Pictures"
However, after I run
path.exists(folder)
The result is false.
What should is the path I should set to folder?
The ~ character signifying the home directory is a shell convention, and doesn't just work outright in Python.
Use the os.path.expanduser() function to have it interpreted like the shell would:
import os.path
folder = os.path.expanduser("~/usr/my_name/Pictures")

How can I specify a path on a Mac

I'm using Python 2.7.2. and I want to open and use a dictionary I created in my shell. My problem is, when I try to import this dictionary into my shell it can't find the file because python is just looking into the 'my documents' folder.
My question is, how can I navigate to the correct folder (just one folder further in 'my documents' folder.
I am using a Macintosh.
You can add custom path to your script as:
import sys
sys.path.append('/Users/username/')
Another way is to set the PYTHONPATH environment variable to /User/username in your shell. Since you know about your shell, I expect that you already know how to edit your shell resource script. You could also add it to your .profile file in which case it should be available even if you change which shell you happen to be using.
If you don't want to "hardwire" the system path, but want to use a relative folder, you could get the name of the working directory and add your subdirectory to that...
subdirname = 'myfolder'
curdir = os.getcwd()
newdir = os.path.join(curdir,subdirname)
sys.path.append(newdir)

Categories

Resources