简体   繁体   中英

Changing Directory With Python, Directory Not Found Error

I am trying to os.chdir() into system32 from python on windows, but when i attempt to change into this directory I am getting this error:

FileNotFoundError: [WinError 2] The system cannot find the file specified: 
'/System32/

So obviously Python can't see this directory but I don't know why because os.listdir() shows this directory in the list. Does this have to do with the permissions that python has? Ultimately my goal is to change into the winevt directory to pull and dump the log files and to check for any errors, so any way to grab these is completely fine. My intuition was simply to change into the directory, open and read the log files and then check for errors, then print and report those errors.

Your current working directory may be different from the one where folder is.

Use this to check your current working directory before changing the directory.

 print('Present DIR is : ',os.getcwd())

Then go to the correct directory and change the directory.

When you try to get into System32, use absolute path rather than the relative path, with the following:

os.chdir(r'C:/Windows/System32')

or in your case:

os.chdir(r'C:\\Windows\\System32\\winevt\\Logs')

As Archit said, you might not be in the correct directory.

The solution to this problem was a little bit hard to come by. I first tried uninstalling python 32 bit but that just broke everything.

I eventually installed python36 and added the python36.dll and the location of this dll to the user and system path (on Windows). Then I made sure to remove anything in the path involving python 34 or python36-32 which is the 32 bit version of python. This then allowed my to easily os.chdir into system32

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM