The problem I have is that when I am in my terminal and I am at the base. I want to for instance change my directory to downloads I was wondering how I would do that because then after I would change my directory I would be able to access certain files in download and tell the computer that I want all the information in this file so my code can run properly.
Thank you
You could try to use os;
os.system('cd ./Downloads')
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.
Hi everyone I'm trying to get Python configured on an OS X laptop and I'm having some trouble. I'm both new to Python and am very unfamiliar with the UNIX terminal. What I'd like to be able to do is to have a directory in my documents folder that would contain python modules and be able to run them from the command line. Currently I have a Python Directory and a chaos.py module inside of it. The full path is /Users/Ben/Documents/Python/chaos.py.
So I followed the steps here and here. I can see that the site-packages for Python 3.4 is in a few spots but I chose this one: '/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/site-packages' to place the .pth file.
I created a file named Ben.pth in this location with the contents: /Users/Ben/Documents/Python/
Now from my (very limited) understanding that should be all I would need to do for Python to start looking right? So I try to run python3 chaos.py in terminal and I get an error:
/Library/Frameworks/Python.framework/Versions/3.4/Resources/Python.app/Contents/MacOS/Python: can't open file 'chaos.py': [Errno 2] No such file or directory
I'll also try opening IDLE clicking File->Open Module... and try to open it from there and I'll recieve a "module not found" box.
I'm completely stumped, I'm not sure if its a syntax error that I made somewhere (again I don't really know what I'm doing with the UNIX commands) or if I'm just way out in right field. If anyone could help me out, I would greatly appreciate it! Thanks!
Forget the .pth stuff for now, that's not something you'd normally do. In a unix-ish environment, the typical way you'd run a script would be to change directory:
cd /Users/Ben/Documents/Python/
and then run the script:
python chaos.py
Another way to do it would be to run the script with an absolute path; you can do this no matter your current working directory:
python /Users/Ben/Documents/Python/chaos.py
Finally, if you've written a utility script you want to be run from anywhere without typing that absolute path all the time, you can do something a little fancier...
Add a 'shebang' line as the first line of your script. It'll go like this:
#!/usr/bin/env python
Get into the directory where your script lives:
cd /Users/Ben/Documents/Python/
Make the script executable:
chmod +x chaos.py
Put a link to the script in a directory on your path... /usr/local/bin/ could be a good choice:
ln -s /Users/Ben/Documents/Python/chaos.py /usr/local/bin/chaos.py
Now you can type chaos.py anywhere on your system and it'll run.
I am currently reading the book Learn Python the Hard Way by Zed Shaw. On exercise 1 after learning the basics of Powershell we must open a notepad++ saved .py file in Powershell. Now here is the problem I'm having.
I am supposed to open this python file by running this command:
PS C:\Users\Trevor> python ex1.py
Zed Shaw does this in his book and it prints back what the file contains...("hello world")
Now i understand my path is wrong by the error message i receive telling me that python is not recognized. I have tried many many times to get the correct path to open python. I have saved the python27 file to my desktop and just about everything trying to get a path that will work.
I am starting at:
PS C:\Users\Trevor>
Any idea how to get to my python27 file and open python when it is saved to my desktop? I also have my ex1.py file saved to my python27 folder. Should i remove it? please help if you can thanks!
PowerShell cannot find python in the current directory or in the directories specified in PATH environment variable.
You can add your installed python directory to PATH variable in System Properties (Win+R → type in sysdm.cpl) → Advanced → Environment variables... → Under "user variables for ...", find PATH variable (if there aren't any, create it using New...), double click to edit it and insert "<your python path>;" (without the quotes). After that, restart PowerShell, run your command and you're done.
I also had this problem, but for me the solution to it wasn't creating a path. I typed in the same code as you, and this is the error message I got:
C:\Users\UserName\AppData\Local\Programs\Python\Python36\python.exe: can't open file 'ex1.py': [Errno 2] No such file or directory
The fix to this was a lot easier than I expected it to be, but it took me a little bit longer to figure out what it was since I just started learning how to work with Python and PowerShell.
This is what you need to do to solve this issue:
Instead of typing the name of the file you are trying to open, you first need to open the directory (the folder where all of your python files are stored).
To open the directory, type in cd DirectoryName and then press enter.
After that, type in "python" (without the quotes) and then the file name (for example, ex1.py). Then, press enter.
At this point, PowerShell should be able to open the file.
This is what you should see:
PS C:\Users\UserName> cd DirectoryName
PS C:\Users\UserName\DirectoryName> python FileName.py
(Note: This is when the file would print)
If your file still isn't opening, you may need to be more specific about the path to the directory. I have my directory located in C:\Users\UserName. If your directory is in C:\Users\UserName but you have it embedded within another folder, you may have to open that folder in PowerShell (you can do this by typing cd FolderName) before you can open the directory folder that's inside it. The easiest thing to do is to save your directory folder in the path C:\Users\UserName so that opening files will take less time/typing.
I am a Python rookie and I learn Python3.3 in Windows.
I wrote my first script, a script to rename files with incremental filenames. But by now, I had to copy my rename.py script to the directory I want to change the files names and run Python rename.py. It is not a very fancy way to use.
I can improve my code and pass the target directory to run my script in origin directory like Python rename.py .../TargetDir, but I have to copy the directory everytime.
So I want to make my script a system command, then I would only type rename in cmd.exe in the directory I want to rename a bunch of files. How can I approach this.
For this purpose, you'll want to use doskey, which allows you to set aliases for commands.
You use it like this:
doskey macroName=macroDefinition
So you would want to write something like this:
doskey rename=Python rename.py .
Where the . stands for the directory you're currently in. (I wasn't exactly clear on what you wanted -- the way I read your question was that you just want to cd into the directory where you want to rename a bunch of files, then run the script.)
Use sys.argv to get the command line arguments. For example test.py:
import os
import sys
path = sys.argv[1]
print(os.listdir(path))
and then you can create a batch file which should placed in a folder that belongs to the PATH variable. In order to do so, create a text document with the following contents and save it as ListDir.bat. Copy the ListDir.bat to either your python folder, or Windows folder (both should be in your PATH)
ListDir.bat:
python C:\test.py "%CD%"
PAUSE
The %CD% refers to the current directory in the windows prompt. So assuming the python script test.py is in C:\ the first line executes the test.py script with the argument current directory.
I used PAUSE to get user input before completing the script, you could choose not to.
After you save the ListDir.bat file. You can navigate to the folder you want to use it in, and just call ListDir
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