Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

No such file or directory when starting directory name with /

Tags:

python

I am just confusing that when I apply:

os.listdir("HW-3/data_sets/")

It works well! and list all the name of files under that directories.However, when I added an additional slash. In my mind, it should be fine.

os.listdir("/HW-3/data_sets/")

But it shows error message:

OSError: [Errno 2] No such file or directory: '/HW-3/data_sets'

It let me think about this program is so unstable. How can I let it work well even when user input '/HW-3/data_sets'? Thanks in advance!

like image 543
Haoyu Chen Avatar asked Jan 09 '23 13:01

Haoyu Chen


1 Answers

A leading slash in a path means an absolute path, or a path that starts at the root of your filesystem. No leading slash makes the path relative to your working directory (typically wherever you launched the script from).

Because of this, the initial slash results in a path that is semantically different from no leading slash, so you should not expect the script to behave the same way for those two paths.

like image 145
nneonneo Avatar answered Jan 25 '23 17:01

nneonneo