Is this good way to get path to desktop? - python

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).

Related

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)

How to add a folder to the Windows PATH temporarily with Python?

How can I add multiple folders to the windows PATH in Python on Windows?
I have multiple programs (wget for windows, phantomjs, casperjs, ...) that I want to use from a python script. And I think it is a good idea to add these folders to the PATH and remove them when the script is ended but I don't know if it's possible...
%PATH% is an environment variable, which is visible in Python by doing this:
import os
print(os.environ['PATH'])
this is a string, which you can make arbitrary modifications to. So, you might do this:
os.environ['PATH'] += ';C:\\wget'
Any modifications you make will only be visible in your script, and any other processes that you launch from it - you don't need to remove the modifications after you're done just to stop them persisting in the wider OS.
import sys
if "C:\\My_Python_Lib" not in sys.path:
sys.path.append("C:\\My_Python_Lib")

Finding a file's directory address on a Mac

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.

Categories

Resources