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
Related
I have set up the environment variable correctly and I am in the correct directory and I am still getting the following error:
C:\Users\Sherw\AppData\Local\Programs\Python\Python310\python.exe: can't open file 'C:\\Users\\Sherw\\OneDrive\\Desktop\\PycharmProjects\\Lab07\\venv\\Scripts\\cowsay.py': [Errno 2] No such file or directory
the cowsay.py file is in the directory C:\Users\Sherw\OneDrive\Desktop\PycharmProjects\Lab07\venv\Scripts so as you can see I am running the file from the correct directory.
'C:\Users\Sherw\AppData\Local\Programs\Python\Python310' I have added this path to the user environment variable.
I dont know why I am still getting the error.
Please let me know the solution to this problem.
Thank you very much.
Try relocating the cowsay.py into another folder alongside its dependencies, e.g. the downloads folder.
Open a cmd window and cd into C:\\Users\\YOURNAME\\Downloads(change YOURNAME to your usename)
Now use the command where python to get the path of the python executable you are actually running. Make sure that is where you think python.exe should be. Also at this point make sure you have installed python from python.org and NOT the microsoft store. If you installed from microsoft store uninstall from control panel and download python here instead. Delete any PATH changes you made beforehand and reboot if you needed to reinstall python.
After go back, open a cmd window and cd back into C:\\Users\\YOURNAME\\Downloads(change YOURNAME to your usename), and type python cowsay.py which should run. Try again in your venv location, although it is a really unconventional place to put your file in. If it works in the Downloads folder only it is an environment error and you may want to recreate the venv.
Whenever I do a project for computer science, I have to make sure all of my files are located in the same folder, or I'll have errors. If I want to use a file from somewhere else, I have to insert it into the path. I do these things but don't fully understand what is happening or why. Why is the path changed in the runtime environment?
When you run a python script you are executing it in the current working directory /home/user/python.py for example. That means this script since it lives in /home/user has access to everything in that path. However you should be able to access any other directory from here as long as the permissions are setup right. You would do that by using relative paths. so for example /home/user/python.py could access a file that is /home/example/file.txt by giving it the path ../example/file.txt from the python project.
Have you tried adding the path using sys.path.append? If you don't want to do that every time then you can set (Windows) %PYTHONPATH% to include your custom path. That's what I do for my include folder.
I am using Python 3.3.1 on Windows 7.
I can execute my python script with this command from the command line
C:\Users\gyorulmaz\workspace\Test_Automation\Source>python TestAutomation.py
however if I try launching python from a different location
C:\python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py
I get an error message
File "C:\Python33\lib\configparser.py", line 1136, in _unify_values
raise NoSectionError(section) configparser.NoSectionError: No section: 'automation'
I am guessing I need to configure something with my python environment.
Help on this would be much appreciated.
Although it's impossible to be sure when you don't show us any code, most likely the problem is that the script is trying to open a file in the same directory as the script (or in a subdirectory or other relative path).
For example:
f = open('spam.txt')
That's not a file in the same directory as the script, that's a file in the current working directory. So, unless the current working directory happens to be the directory the script is in, you will get a FileNotFoundError.
In your case, you're using configparser, which will treat a FileNotFoundError the same as an empty file, so it will successfully import an empty config—but then as soon as you ask for the 'automation' section, there will obviously be no such section, so you'll get a NoSectionError instead.
Normally, you're going to want to install a script somewhere, and install whatever data files it needs somewhere as well, rather than running them out of the development directory. Python has all kinds of ways to do that, but you're going to have to read a tutorial about packaging things up in Python (I think Hitchhiker's Guide to Packaging is still the recommended starting point, but that may be out of date, so don't quote me on it…) before I could even begin to explain how to package up data files.
But here, we're talking about config files. Normally, these just go to some fixed path relative to the user's home directory. So, for a quick&dirty solution, you could just pick some such home-relative path and hardcode it into your app. Or, to get even quicker and dirtier, if this is just a hacky script for your own use, just hardcode the absolute pathname.
If you really, really want to use a script-relative path, and don't want to hardcode it:
scriptdir = os.path.dirname(sys.argv[0])
configpath = os.path.join(scriptdir, 'my.cfg')
Unfortunately, sys.argv[0] is not guaranteed to be an absolute pathname, or even a pathname at all; it's allowed to be just the filename. In Python 3.4, you can solve this by using __file__ instead, but you're using 3.3, and that has other problems. (See this question for details.) And what if someone puts your script inside a zip archive and runs it out of the archive (which Python can do for you)? There is no perfect answer here, but the code above is probably what you want.
Are you running C:\python executable --OR running python at c:\ location?
Seems like you are not. You're running a file c:\python (which doesn't exist). If you'll run it from C:\, then you would have pasted it like C:\>python .... or never asked this ?.
Do this:
1. cd to C:\
2. Now you'll see C:> as prompt in DOS/Windows
3. Run the following and it should work.
python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py
Your screen will show it like:
C:\>python C:\Users\gyorulmaz\workspace\Test_Automation\Source\TestAutomation.py
OR
if you did a typo, then you can seek help from Python - Find Path to File Being Run
I'm new and I have no idea where the default directory for the open() function is.
For example open('whereisthisdirectory.txt','r')
Can someone advise me? I've tried googling it (and looking on stackoverflow) and even putting a random txt file in so many folders but I still can't figure it out. Since I'm beginning, I want to learn immediately rather than type "c:/directory/whatevevr.txt" every time I want to open a file. Thanks!
Ps my python directory has been installed to C:\Python32 and I'm using 3.2
os.getcwd()
Shows the current working directory, that's what open uses for for relative paths.
You can change it with os.chdir.
If you working on Windows OS first type
import os
then type
os.getcwd()
and it should print the current working directory.
The answer is not python-specific. As with programs written in any other language, the default directory is whatever your operating system considers the current working directory. If you start your program from a command prompt window, the CWD will be whatever directory you were in when you ran the program. If you start it from a Windows menu or desktop icon, the CWD is usually defined alongside the program's path when creating the icon, or else falls back to some directory that Windows uses in the absence of that information.
In any case, your program can query the current working directory by calling os.getcwd().
The default location is the CWD (Current Working Directory), so if you have your Python script in c:\directory and run it from there, if you call open() it will attempt to open the file specified in that location.
First, you must import:
import os
Then to print the current working directory:
os.getcwd()
If you want to change the current working directory:
os.chdir('your_complete_required_path')
create the .txt file in the directory where u have kept .py file(CWD) and run the .py file.
The open() function for file always creates files in the current working directory. The best way to find out your current working directory is to find three lines of small code:
import os
current_working_directory = os.getcwd()
print(current_working_directory)
Run this above code and you will get your current working directory where open() function creates a new file. Good Luck!
If you’re running your script through an interpreter (i.e pycharm, VSCode etc) your Python file will be saved, most likely, in my documents (at least in VSCode, in my personal experience) unless you manually save it to a directory of your choosing before you run it. Once it is saved, the interpreter will then use that as you current directory so any saves your Python script will create will also automatically go there unless you state otherwise.
it depends on how you run it from the terminal
like this, it is going to look in your home directory
C:\Users\name>python path\file.py
and like this, it is going to look next to your file
C:\Users\name>cd path
C:\Users\name\path>python file.py
I have a process which is loading a DLL from a place not listed in the documented search order (docs linked below). I want to know why.
Here's the description of my setup:
I have a folder 'c:\foo' containing a.dll and b.dll.
I have a python script also stored in c:\foo.
The python script does a LoadLibrary('c:/foo/a.dll') (via ctypes)
a.dll is linked against the import library for b.dll (ie using implicit linking).
I run the python script with a current directory of, say, c:. It could be anything.
b.dll is loaded from c:\foo, even though that isn't on the search path.
Looking at the process monitor trace, I can see that all the documented search paths were tried first, and all failed. Then the python process tried and failed to open "C:\WINDOWS\assembly\GAC\Microsoft.VC80.CRT.mui\8.0.50727.4053_en-US_1fc8b3b9a1e18e3b\Microsoft.VC80.CRT.mui.DLL", then it opened c:\foo\b.dll.
So, it seems to be that the a.dll's directory is being searched for b.dll even though the docs don't say it should be. Also, this happens after looking on the system path, which is mad. Can anyone shed any light on this?
The same thing happens with a MatLab script that also uses a.dll.
I'm running Windows XP SP 3.
This MSDN article explains the default search order. I quote:
The directory specified by lpFileName.
The system directory. Use the GetSystemDirectory function to get the path of this directory.
The 16-bit system directory. There is no function that obtains the path of this directory, but it is searched.
The Windows directory. Use the GetWindowsDirectory function to get the path of this directory.
The current directory.
The directories that are listed in the PATH environment variable. Note that this does not include the per-application path specified by the App Paths registry key. The App Paths key is not used when computing the DLL search path.
a.dll is probably using runtime dynamic linking as a last resort
http://msdn.microsoft.com/en-us/library/ms686944%28VS.85%29.aspx