What is the difference between "./" and "../" when using os.path.isdir ()?

I am new to python. And something bothers me today. There are several folds to the c:\python\ path. I am editing a python script along this path and running the code:

 for dir_name in os.listdir("./"): print dir_name print os.path.isdir(dir_name) 

He prints:

 Daily True renafile.py False script True 

But when I put the script in fold Daily , which is under the c:\python\ path, and ran the code:

 for dir_name in os.listdir("../"): print dir_name print os.path.isdir(dir_name) 

He prints:

 Daily False renafile.py False script False 

Did they have a difference?

+7
python
source share
1 answer

It returned false because when you call isdir with the name of the folder, python looks for that folder in the current directory - unless you specify an absolute path or relative path.

Since you specify the files in "../" , you should call isdir like this:

 print os.path.isdir(os.path.join("../", dir_name)) 

You can change your code to:

 list_dir_name = "../" for dir_name in os.listdir(list_dir_name): print dir_name print os.path.isdir(os.path.join(list_dir_name, dir_name)) 
+9
source share

All Articles