I am trying to write code that uses a recursive function to search for files in a directory, and returns the path to the file that matches the search term. However, I keep getting this error when I use "../.." as the path name "PermissionError: [WinError 5] Access is denied: '../..\\AppData\\Local\\Application Data'".
import os
def main():
pathname=input('Please enter path name: ')
filenameinput=input('Please enter file name: ')
def disk_usage(path):
if os.path.isdir(path):
for filename in os.listdir(path):
childpath = os.path.join(path, filename)
if os.path.isdir(childpath):
disk_usage(childpath)
else:
if childpath.endswith(filenameinput):
print(childpath)
#return
disk_usage(pathname)
main()
I should not need to use os.walk()
for this. I have it working but it returns several paths ending in the filename I specified and then returns the WinError 5 thing.
You're getting a permission error because Application Data
is not a real folder in Windows 7+, it's a "junction" (symlink in Unix-speak) pointing to C:\\Program Files
. It only exists for backwards compatibility.
You have two options:
You can read the junction with some Windows-specific native code, through win32file
. See this SO answer.
You can catch the permission error, and ignore it (maybe print a warning message). This is probably the better option unless you really need to read this folder.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.