[Tutor] File path requirements (absolute or relative)?

Mats Wichmann mats at wichmann.us
Fri May 15 16:35:59 EDT 2020


On 5/15/20 2:31 PM, boB Stepp wrote:
> My file structure for a project is as follows:
> 
> ~
>     | Projects
>         | project_name
>             | docs
>             | src
>                 project_name.py
>             | tests
>                 test.py
>                 test_csv_file.csv
> 
> I wish to pass the path for "test_csv_file.csv" from the test.py program to
> the function in src/project_name.py which processes csv files.  Ideally I
> would like to use a relative path name like "../tests/test_csv_file" and
> have the function in src/project_name.py to be able to use it.  But so far
> all of my efforts to use a relative path name have failed.  Even
> "~/Projects/project_name/tests/test_csv_file.csv" fails.  Only when I use
> the absolute path name
> "/Home/bob/Projects/project_name/tests/test_csv_file.csv" does the test
> complete successfully.  What am I misunderstanding?
> 

~username is not interpreted by Python directly, it's a shell thing.

For Python you can use os.path.expanduser or pathlib's Path.home()

for the rest - if a relative path doesn't work, it usually means it
isn't being evaluated in the context you think it is.  relative paths
certainly work, so you're likely getting them relative to someplace else.

some debugging prints should help...


More information about the Tutor mailing list