You can use the package appdirs
. This is developed by ActiveState who must have quite a lot of experience on cross-platform python.
import appdirs
appdirs.user_config_dir(appname = 'MyApp')
import os
print os.path.expanduser("~/.my_config/data")
oftentimes on windows this is
os.path.expandvars("%appdata%/.my_config")
In this article, we learned two different ways to get the home directory of a user's system in Python. One way was using os.path.expanduser('~') and another way was pathlib.Path.home(). Do check your script to avoid errors.,In this article, we will learn how to get the path of the home directory in Python. We will use two built-in functions to get the home directory.,The home directory contains multiple files for a given user of the system. Look at the two below scripts to get the home directory in Python. We will look at two different modules of Python such as the os module and pathlib module.,The pathlib module provides path.home() to get the home directory in Python. This function works fine if your Python version is Python 3.4+. It returns a new path object having the user’s home directory.
os module provides os.path.expanduser('~')
to get the home directory in Python. This also works if it is a part of a longer path like ~/Documents/my_folder/. If there is no ~
in the path, the function will return the path unchanged. This function is recommended because it works on both Unix and Windows. It returns the argument with an initial component of (tilt) ~
or ~user
replaced by the user’s home address.
import os
print(os.path.expanduser('~'))
The pathlib module provides path.home()
to get the home directory in Python. This function works fine if your Python version is Python 3.4+. It returns a new path object having the user’s home directory.
from pathlib
import Path
print(Path.home())
A mapping object where keys and values are strings that represent the process environment. For example, environ['HOME'] is the pathname of your home directory (on some platforms), and is equivalent to getenv("HOME") in C.,Return the effective group id of the current process. This corresponds to the “set id” bit on the file being executed in the current process.,Possible values for the mode parameter to the spawn* family of functions. If either of these values is given, the spawn*() functions will return as soon as the new process has been created, with the process id as the return value.,Call the system initgroups() to initialize the group access list with all of the groups of which the specified username is a member, plus the specified group id.
for fd in range(fd_low, fd_high):
try:
os.close(fd)
except OSError:
pass
if os.access("myfile", os.R_OK):
with open("myfile") as fp:
return fp.read()
return "some default data"
try:
fp = open("myfile")
except PermissionError:
return "some default data"
else:
with fp:
return fp.read()
with os.scandir(path) as it:
for entry in it:
if not entry.name.startswith('.') and entry.is_file():
print(entry.name)
>>>
import os
>>>
statinfo = os.stat('somefile.txt') >>>
statinfo
os.stat_result(st_mode = 33188, st_ino = 7876932, st_dev = 234881026,
st_nlink = 1, st_uid = 501, st_gid = 501, st_size = 264, st_atime = 1297230295,
st_mtime = 1297230027, st_ctime = 1297230027) >>>
statinfo.st_size
264
os.stat in os.supports_dir_fd
Python lets you use OS-X/Linux style slashes "/" even in Windows. Therefore, you can refer to the file as 'C:/Users/narae/Desktop/alice.txt'. RECOMMENDED. ,Alternatively, you can prefix the entire file name string with the rawstring marker "r": r'C:\Users\narae\Desktop\alice.txt'. That way, everything in the string is interpreted as a literal character, and you don't have to escape every backslash. ,In a Python script: When you execute your script, your CWD is set to the directory where your script is. Therefore, you can refer to a file in a script by its name only provided that the file and the script are in the same directory. An example: myfile = open('alice.txt') # alice.txt is in the same dir as foo.py mytxt = myfile.read() myfile.close() foo.py ,If using backslash, because it is a special character in Python, you must remember to escape every instance: 'C:\\Users\\narae\\Desktop\\alice.txt'
>>> myfile = open('C:/Users/narae/Desktop/alice.txt') # Windows >>> mytxt = myfile.read() >>> myfile.close()
>>> myfile = open('/Users/narae/Desktop/alice.txt') # Mac and Linux >>> mytxt = myfile.read() >>> myfile.close()
myfile = open('alice.txt') # alice.txt is in the same dir as foo.py mytxt = myfile.read() myfile.close() foo.py
>>> import os >>> os.getcwd() 'D:\\Lab' >>> os.chdir('scripts/gutenberg') # relative path: scripts dir is under Lab >>> os.getcwd() 'D:\\Lab\\scripts\\gutenberg' >>> os.chdir(r 'D:\Corpora\corpus_samples') # absolute path, using\ and r prefix >>> os.getcwd() 'D:\\Corpora\\corpus_samples'
Updated: September 23, 2020
# Import necessary packages import os import earthpy as et
# Direction and number of slashes are handled by the
function
os.path.join("earth-analytics", "data")
'earth-analytics/data'
# Check that a directory exists on your computer my_path = os.path.join("earth-analytics", "data") # Boolean output(True or False) os.path.exists(my_path)
False
# Find your home directory et.io.HOME