简体   繁体   中英

How can you harvest the current user and script's working directory in python?

Currently, I am statically specifying a filepath for the source data for a script using the snippet below:

 def get_files():
     global thedir
     thedir = 'C:\\Users\\username\\Documents'
     list = os.listdir(thedir)
     for i in list:
         if i.endswith('.txt'):
             print("\n\n"+i)
             eat_file(thedir+'\\'+i)

The reason that I am statically assigning the location is that the script is failing to execute correctly when it is executed in a debugging environment such as Eclipse & Visual Studio Code. These debuggers assume that the script is being ran from their working directories.

Since I'm unable to modify the local settings of every system that might be running this script, is there a recommended module to force the script to harvest either the active user (linux and windows) and/or the script's local directory pragmatically?

The new-ish pathlib module (available in Python >= 3.4) is great for working with path-like objects (both Windows and for other OSes). Use it. Don't bother with the outdated os module. And don't bother trying to use naked strings for representing path-like objects.

It's Paths - Paths all the way down

To simplify: you can build up any path (directory and file path objects are treated exactly the same) as an object, which can be an absolute path object or a relative path object .

Simple displaying of some useful paths- such as the current working directory and the user home- works like this:

from pathlib import Path

# Current directory (relative):
cwd = Path() # or Path('.')
print(cwd)

# Current directory (absolute):
cwd = Path.cwd()
print(cwd)

# User home directory:
home = Path.home()
print(home)

# Something inside the current directory
file_path = Path('some_file.txt') # relative path; or 
file_path = Path()/'some_file.txt' # also relative path
file_path = Path().resolve()/Path('some_file.txt') # absolute path
print(file_path)

To navigate down the file tree, you can do things like this. Note that the first object, home , is a Path and the rest are just strings:

file_path = home/'Documents'/'project'/'data.txt' # or
file_path = home.join('Documents', 'project', 'data.txt')

To read a file located at a path, you can use its open method rather than the open function:

with file_path.open() as f:
    dostuff(f)

But you can also just grab the text directly!

contents = file_path.read_text()
content_lines = contents.split('\n')

...and WRITE text directly!

data = '\n'.join(content_lines)
file_path.write_text(data) # overwrites existing file

Check to see if it is a file or a directory (and exists) this way:

file_path.is_dir() # False
file_path.is_file() # True

Make a new, empty file without opening it like this (silently replaces any existing file):

file_path.touch()

To make the file only if it doesn't exist , use exist_ok=False :

try:
    file_path.touch(exist_ok=False)
except FileExistsError:
    # file exists

Make a new directory (under the current directory, Path() ) like this:

Path().mkdir('new/dir') # get errors if Path()/`new` doesn't exist
Path().mkdir('new/dir', parents=True) # will make Path()/`new` if it doesn't exist
Path().mkdir('new/dir', exist_ok=True) # errors ignored if `dir` already exists

Get the file extension or filename of a path this way:

file_path.suffix # empty string if no extension
file_path.stem # note: works on directories too

Use name for the entire last part of the path (stem and extension if they are there):

file_path.name # note: works on directories too

Rename a file using the with_name method (which returns the same path object but with a new filename):

new_path = file_path.with_name('data.txt')

You can iterate through all the "stuff' in a directory like so using iterdir :

all_the_things = list(Path().iterdir()) # returns a list of Path objects

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM