>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
Also works if it is already an absolute path:
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
Answer from sherbang on Stack OverflowHelp understanding relative paths in python
How to get an absolute file path in Python - Stack Overflow
Total Noob - How do I properly refer to a file path in python?
Adding Python to PATH on Windows - Stack Overflow
Is SYS path the same as Pythonpath?
How do I create a Pythonpath variable?
How do I add an environment variable in Pythonpath?
Videos
I have a project roughly setup like so
project -data --input.txt -src --main.py
inside main.py I have a line
filename = '../data/input.txt'
and then I do logic with that data
my question is the following:
if I run from the command line
project$ src/python main.py
I get a FileNotFoundError on '../data/input.txt'
However if I run this from the command line
project/src$ python main.py
the function behaves as expected.
Can someone explain what is happening? Also what can I do to make both commands work?
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
Also works if it is already an absolute path:
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
'C:/example/cwd/mydir/myfile.txt'
You could use the new Python 3.4 library pathlib. (You can also get it for Python 2.6 or 2.7 using pip install pathlib.) The authors wrote: "The aim of this library is to provide a simple hierarchy of classes to handle filesystem paths and the common operations users do over them."
To get an absolute path in Windows:
>>> from pathlib import Path
>>> p = Path("pythonw.exe").resolve()
>>> p
WindowsPath('C:/Python27/pythonw.exe')
>>> str(p)
'C:\\Python27\\pythonw.exe'
Or on UNIX:
>>> from pathlib import Path
>>> p = Path("python3.4").resolve()
>>> p
PosixPath('/opt/python3/bin/python3.4')
>>> str(p)
'/opt/python3/bin/python3.4'
Docs are here: https://docs.python.org/3/library/pathlib.html
Hello,
I am trying to write in the path "c:/gpt-2-2/models" into a python code. When I run the code I get a syntax error at the "C:" part. I guess I don't know how to properly write file paths in python. Any help would be appreciated.
- Hold Win and press Pause.
- Click Advanced System Settings.
- Click Environment Variables.
- Append
;C:\python27to thePathvariable. - Restart Command Prompt.
When setting Environmental Variables in Windows, I have gone wrong on many, many occasions. I thought I should share a few of my past mistakes here hoping that it might help someone. (These apply to all Environmental Variables, not just when setting Python Path)
Watch out for these possible mistakes:
- Kill and reopen your shell window: Once you make a change to the ENVIRONMENTAL Variables, you have to restart the window you are testing it on.
- NO SPACES when setting the Variables. Make sure that you are adding the
;C:\Python27WITHOUT any spaces. (It is common to tryC:\SomeOther; C:\Python27That space (␣) after the semicolon is not okay.) - USE A BACKWARD SLASH when spelling out your full path. You will see forward slashes when you try
echo $PATHbut only backward slashes have worked for me. - DO NOT ADD a final backslash. Only
C:\Python27NOTC:\Python27\