My script need scan a netdisk folder to get the content of it.
I use os.listdir() method for an easy implement, then I got permission error when executing with Python 3.x, but it's working fine with Python 2.7, I wanna know what's difference of this method implement between 3.x and 2.7? Why there is the permission error in 3.x?
I am stuck in this problem for a long time, and it's force us to turn back to use Python 2.7 now, but I really don't want to use 2.7 because of it's bad encoding problem.
Do you guys know why?
Asked
Active
Viewed 298 times
0

Ryan_D
- 11
- 1
-
`.mp3`? Is `os.listdir` actually appropriate here? Is `alps-mp-p0.mp3` a folder or something else? Also, avoid using images, It is easier to work with actual code as text to see what the problems are. – Paritosh Singh Mar 08 '19 at 08:25
-
actually, it's part of the folder name, please check the screenshot, it's working fine in Python2.7 with the same method call. – Ryan_D Mar 08 '19 at 08:30
-
run as admin and see if that solves the issue. From your description however, something seems wrong, you say its part of the folder name, but os.listdir should only work off of proper folder names, not partial ones. Can you doublecheck if `alps-mp-p0.mp3` isnt actually a folder? – Paritosh Singh Mar 08 '19 at 09:05
-
@Paritosh Singh, thanks for your reply. Yes, I am very sure that the path is correct, and I am also sure that I run the command with Admin account. Did you see that the result of this method is fine in Python2.7 Env (the 2nd screenshot)? That's the strange point in deed. – Ryan_D Mar 08 '19 at 10:33
1 Answers
0
To find all matches based off of a partial file/folder name, you can use the glob module, with a wildcard *
import glob
partial_path = r'L:\ALPS_load\alps-mp-p0.mp3*' #notice the wildcard at the end
listing = glob.glob(partial_path)
print(listing)
Here is a demonstration on my system, which i feel like i should add considering the strange circumstances outlined in the OP question. Assuming your alps-mp-p0.mp3
is actually a partial name, os.listdir should not have worked in python 3 with a very different error. (Is it a hidden folder perhaps?)
import os
os.listdir(r'C:\Users\paritosh.singh\D')
#Output:
FileNotFoundError: [WinError 3] The system cannot find the path specified: 'C:\\Users\\paritosh.singh\\D'
With glob instead,
partial_path = r'C:\Users\paritosh.singh\D*'
listing = glob.glob(partial_path)
print(listing)
['C:\\Users\\paritosh.singh\\Desktop',
'C:\\Users\\paritosh.singh\\Documents',
'C:\\Users\\paritosh.singh\\Downloads']
Hope this helps, but we may be dealing with something else that is missing here.

Paritosh Singh
- 6,034
- 2
- 14
- 33
-
thanks Paritosh, sorry for my unclear expression, in fact the name "alps-mp-p0.mp3" is the full folder name (.mp3 is the partial folder name in my above comment), I just want to get all sub folder name in my given path, and i am sure it is not a hide folder, it shows permission error in python3, but all fine in python2. – Ryan_D Mar 10 '19 at 03:28