Windows Path in Python

Windows path in Python

you can use always:

'C:/mydir'

this works both in linux and windows.
Other posibility is

'C:\\mydir'

if you have problems with some names you can also try raw string literals:

r'C:\mydir'

however best practice is to use the os.path module functions that always select the correct configuration for your OS:

os.path.join(mydir, myfile)

From python 3.4 you can also use the pathlib module. This is equivelent to the above:

pathlib.Path(mydir, myfile)

or

pathlib.Path(mydir) / myfile

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

You should use:

os.environ['PATH'] += R";C:\my\folder"

Python Convert Windows File path in a variable

I've solved it.

The issues lies with the python interpreter. \t and all the others don't exist as such data, but are interpretations of nonprint characters.

So I got a bit lucky and someone else already faced the same problem and solved it with a hard brute-force method:

http://code.activestate.com/recipes/65211/

I just had to find it.

After that I have a raw string without escaped characters, and just need to run the simple replace() on it to get a workable path.

Python windows path

Thanks for all the help, in the end it was solved by removing 1 backslash.

<add key="logfilepath" value="\Logs\LogMain.log" />

to

<add key="logfilepath" value="Logs\LogMain.log" />

How to add a directory to System path variable in Python

sys.path is not the same as the PATH variable specifying where to search for binaries:

sys.path

A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

You want to set the PATH variable in os.environ instead.

app_path = os.path.join(root_path, 'other', 'dir', 'to', 'app')
os.environ["PATH"] += os.pathsep + os.path.join(os.getcwd(), 'node')


Related Topics



Leave a reply



Submit