python rename script not working when executed from finder (Mac) - python

Helly everyone,
I'm kind of new to python, but I have written a little script that helps me rename file extensions.
It's very short, so I'll post it here:
import os
for filename in os.listdir("."):
if filename.endswith("gd"):
os.rename(filename, filename + ".dat")
Now, as I have several directories I need to execute this in, it was getting kind of annoying to always navigate to the folder in terminal and execute it from there using the commmand
python renamescrip.py
Now I stumbled across some advice that would make a python script execute upon double clicking in the Finder, which would make life a lot easier for me, as I would just need to copy and paste the renamescript.py into the folder in which I want to rename the files and double click on it to execute it.
I followed these instructions: http://skillfulness.blogspot.de/2010/12/how-to-run-python-script-from-mac-os-x.html , which show how to create a .command file.
However, when I double clicked the renamescript.command file, it didn't change the file names in the directory.
I also tried opening the .py file with Python Launcher, but that also didn't change the filenames.
Both times a terminal window opened, though, with the message:
cd '/path to directory with files to rename/' && '/usr/local/bin/pythonw' '/path to directory with files to rename/renamescript.py' && echo Exit status: $? && exit 1
Exit status: 0
logout
or if I click on the .command file it's just:
/path to directory with files to rename/renamescript.command ; exit;
logout
What am I doing wrong?

When you double click, you are not running with the same directory form your current directory as when you run from the shell. This causes your os.listdir(".") to not find the files you think it should. You should change your directory at the beginning of your script with
os.chdir(os.path.dirname(os.path.abspath(__file__)))
before your os.listdir call

Related

subprocess.popen not able to run command [duplicate]

This question already has answers here:
How can I specify working directory for a subprocess
(2 answers)
Closed 1 year ago.
I have an exe located at C:\Users\srinivast6>C:\Users\srinivast6\Documents\Cipia\Cipia\DriverSenseCLI-v7.4.3-win64.
This exe I can run in terminal (non admin mode ,windows OS) without any problem.
I am calling it programmatically using subprocess.Popenas below.
process = subprocess.Popen(['myApplication.exe'])
But it is giving following error. It is not able to read license file. What might be the cause for this?. Do I need to open this in admin mode?
Cannot open license file : license.dat
License 284
Error initializing library:
license is not valid
EDIT1: After changing the directory from where I was running python script to the directory where exe is located,I no more see any error. It is launching as expected. But I am packing this python script as an standalone executable. So the user might use this executable from any directory to launch myApplication.exe. I cannot actually put restriction on user.
So is it possible to set the current working directory programmatically to the path where myApplication.exe is located??
Some more information needs to be shared, but a guess for what could be happening is that you are executing the python script from a directory other than where you normally execute myApplication.exe, and that the call to open the file in myApplication is using a relative path (which seems to be the case here). That would mean when you execute myApplication with Popen, it will have the working directory set to the working directory of wherever you executed the python script and the relative path would be relative to that. If that is the case, try executing the script from the same directory as where you would usually execute myApplication.exe or change the path passed to file the file open call in myApplication to use an absolute path.
Example:
Directory Structure
C:\Users\user
|--popen.py
|--somedir
|--license.dat
|--myApplication.exe
|--myApplication.py
Contents of popen.py:
process = subprocess.Popen(['myApplication.exe'])
Contents of myApplication.py (I realize python files don't get compiled to executables, but it is only for the sake of an example):
f = open('license.dat', 'r')
Now, this wouldn't work:
cwd: C:\Users\user
$ python popen.py # File not found error.
Either execute the script from somedir:
cwd: C:\Users\user\somedir
$ python ..\popen.py
Or alternatively, change the path passed to open in myApplication.py:
f = open('C:\Users\user\somedir\license.dat', 'r')

How to run python script as executable and keep access to files surrounding it?

I have a python program "Alfred.py" that I want to be able to run by clicking an icon or typing "Alfred" in the terminal. It is connected to a database file and pulls in Excel files when asked to. When I try to run it by clicking, this is what I get:
mcaay:~ mcaay$ /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.desktop ; exit;
/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.desktop: line 1: [Desktop: command not found
Traceback (most recent call last):
File "/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.py", line 59, in <module>
AND Usterka IS NOT NULL;""")
sqlite3.OperationalError: no such table: repairs
logout
Saving session...
...copying shared history...
...saving history...truncating history files...
...completed.
[Process completed]
This line:
sqlite3.OperationalError: no such table: repairs
means that Alfred.py didn't find .db file, created it but it was empty so there was no table called "repairs". This .db file sits in the same directory as Alfred.py.
What I did until now:
added "#!/usr/bin/env python3" as the 1st line of Alfred.py
typed "chmod +x /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.py" in terminal to make it executable
added "export PATH=$PATH:/Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/" to my .bash_profile to make Alfred.py available from any place in terminal
created Alfred.desktop file for a clickable icon:
[Desktop Entry]
Name=Alfred
Exec=python3 /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.py
Terminal=true
Type=Application
made Alfred.desktop executable by typing "chmod +x /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred/Alfred.desktop" in the terminal
When I type in terminal "Alfred.py", I get:
mcaay:~ mcaay$ Alfred.py
Traceback (most recent call last):
File "/Users/mcaay/Documents/Moje Dokumenty/MANTA/Alfred/Alfred.py", line 59, in <module>
AND Usterka IS NOT NULL;""")
sqlite3.OperationalError: no such table: repairs
So the same as with clicking "Alfred.desktop".
If I type just "Alfred", I get:
mcaay:~ mcaay$ Alfred
-bash: Alfred: command not found
I want just typing "Alfred" to work and I want it to see the database and other files correctly, or alternatively if clicking the icon will work properly it is acceptable too. My Googe-Fu failed me here. What do?
P.S. I noticed that a database file is created in my home directory ("/Users/mcaay"), as if Alfred.py was pulled from original destination to some temporary destination and executed there (other files were not pulled so program crashed).
When you tell SQLite where to find the db, you're using a relative path, but that relative path is relative to the current working directory. You need to either change directory inside your script or give SQLite a full path to your db. Either way, this requires the logic for finding where your script is, or you can put the db in another well-known path (e.g., ~/.config/alfred). Personally, I prefer always building full paths, and avoiding relative paths (and the change-directory) option as much as possible, but a chdir to your script path should be relatively safe as well, and may require less change at this point.
I think that it's just looking for the .db file in the directory it was executed in. You should either define a .db with the absolute path or access the .db file in the same directory of the script which you can get using:
os.path.dirname(sys.argv[0])
Ok, so after a good night of sleep the things I've read finally computed in my head and I got an answer. Everything I did before is unnecessary and can be discarded.
My solution (keep in mind I'm using macOS Mojave atm):
In home directory, I created a file called .start_alfred.sh.
This is a shell script that will have a callable command in terminal to go to my specified directory and then run a python script.
Inside, I wrote this:
#!/bin/bash
function Alfred() {
cd /Users/mcaay/Documents/Moje\ Dokumenty/MANTA/Alfred
python3 Alfred.py
}
The name of this function (Alfred) is what will invoke the shell commands included inside. It's basically doing what you would normally do in terminal to start a program.
In my .bash_profile, I added source ~/.start_Alfred.sh at the end, so everytime I open the terminal everything that is in .start_Alfred.sh becomes available immediately.
That's basically it :) Maybe not the cleanest but super easy and good enough for what I need. Tanktalus' and asafpr's answers helped me to come up with this solution.
Use case:
mcaay:~ mcaay$ Alfred
Co jest?
>>> thx
np
mcaay:Alfred mcaay$
Thx is the command to end the python script.

Python can't run absolute script?

I encountered such a weird situation:
naivechou#naivechou/~>python test.py
test
naivechou#naivechou/~>pwd
/home/naivechou
naivechou#naivechou/~>python /home/naivechou/test.py
C:\toolchain\python\python.exe: can't open file '/home/naivechou/test.py': [Errno 2] No such file or directory
My working directory is /home/naivechou/, test.py is in there.
If I run test.py with absolute path, I'll get an error message of No such file or directory.But everything will be fine if I enter that directory and then run it. What's wrong with python?
Try moving into the folder where the python script is located and do a "ls" command there in Linux. if windows then do 'dir'. if you see the required file there then execute the following command
C:\location_where_the_script_is> python yourfile.py
For commands entered on the command line, Windows doesn't recognize forward-slashes as directory separators.
Your second example is looking in the current directory for the literal filename /home/naivechou/test.py, and of course such a filename does not exist.
Use backslashes instead, as is the Windows way:
python \home\naivechou\test.py

Error with Reading Files

I am working through Learn Python the Hard Way but am having a problem with an exercise that reads a file: https://learnpythonthehardway.org/python3/ex15.html
I have followed this and made sure my file is in the same folder as my python file. I keep getting the error:
Traceback (most recent call last):
File "ex15.py", line 5, in <module>
txt = open(filename)
FileNotFoundError: [Errno 2] No such file or directory: 'ex15_sample.txt'
I am entering: python ex15.py ex15_sample.txt into the powershell. I tried looking up the problem I'm having but could not find anything that helped in my case. Let me know if I am doing something wrong or if I'm missing something. Thanks
You can check your current path and whether your file exists by
import os
#get the current working directory
os.getcwd()
#check whether your file exists in the current working directory
os.path.exists("./ex15.py")
try 2 things below
Check both files .py and .txt are at same location. and then execute program.
this is how i ran the code.
C:\Users\db\Desktop\Python>python ex15.py
enter File Name: ex15_sample.txt
This is stuff I typed into a file.
It is really cool stuff.
Lots and lots of fun to have in here.
If your file is at other location you need to provide absolute path for your file. as
C:\Users\db\Desktop\Python>python C:\Users\deepakb\Desktop\ex15_sample.txt
i had my txt file at desktop this time.
A brief demonstration on working directory and relative file locations..
Say we have a text file named hello.txt with the contents:
Hello World!
that is located at "C:\Users\username\Documents\python\hello.txt"
And we have a python script named script.py with the contents:
with open('hello.txt', 'r') as f:
print(f.read())
Which is located at "C:\Users\username\Documents\python\script.py"
If I just hit the windows key and type power shell and hit enter to open it, I'll see it by default gives me a prompt at my home directory: "C:\Users\username>". From here I can execute my python script by calling C:\Users\username> python C:\Users\username\Documents\python\script.py. If I do this, however, they python interpreter would have started in my home folder, and when it tries to find "hello.txt" it will only search through the folder "C:\Users\username". That's not where the file is, so we get a file not found error.
To resolve this we have a few options. Number one is to re-write our script file to include the full path of our text file: ... open("C:/Users/username/Documents/python/hello.txt", ... This way there is no searching necessary, and the script can be run from anywhere. Another option is to change the working directory of the interpreter from within the script. You can set the working directory with the os library by calling os.chdir('C:/Users/username/Documents/python'). Finally you can set the working directory with PowerShell before calling the python interpreter using the cd command:
C:\Users\username> cd .\Documents\python\
I had the same error.
It was a mistake from me naming the file as ex15_sample.txt.
When you are creating a file in notepad, the file name would be automatically .txt
so if you have created a file just rename it as ex15_sample.
or
You can also run the same program by giving python ex15.py ex15_sample.txt.txt
As you can see the file name might be ex15_sample.txt with a .txt extension.

How can I run my python script from the terminal in Mac OS X without having to type the full path?

I'm on Mac OS 10.6 Snow Leopard and I'm trying to add a directory to my PATH variable so I can run a tiny script I wrote by just typing: python alarm.py at the terminal prompt.
I put the path in my .profile file and it seems to show up when I echo $PATH, but python still can't find script the that I've put in that directory.
Here's the contents of my .profile file in my home directory:
~ toby$ vim .profile
export PATH=/Users/tobylieven/Documents/my_scripts:$PATH
Here's the output of echo $PATH, where all seems well:
~ toby$ echo $PATH
/Users/tobylieven/Documents/my_scripts:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin
Here's the script I'm trying to run:
~ toby$ ls /Users/tobylieven/Documents/my_scripts
-rwxrwxrwx# 1 tobylieven staff 276 17 Jan 21:17 alarm.py
Here's the command I'm trying to use to run the script and the fail message I'm getting instead:
~ toby$ python alarm.py
python: can't open file 'alarm.py': [Errno 2] No such file or directory
If anyone has an idea what I might be doing wrong, that'd be great.
Thanks a lot.
PATH is only for executables, not for python scripts. Add the following to the beginning of your Python script:
#!/usr/bin/env python
and run
sudo chmod a+x /Users/tobylieven/Documents/my_scripts/alarm.py
Then, you can type just alarm.py to execute your program.
Which python are you targeting?
Did you install it with brew? It uses a different path.
which python3 or which python
Choose the one you want
Copy that output
Paste it at the top of your python file
add a #! in front of that path so it looks something like
#!/usr/local/bin/python3
Make sure to change the file permissions
chmod +x filename
Put that file in a folder that is in your path
Not sure if your folder is in your path?
echo $path
How to add that folder to your path?
Find your path first
echo $HOME
If you are using bash or zsh you might have something like this
In ~/.bash_profile or ~/.bashrc or ~/.zshrc at the bottom of your file
export PYTHON_UTILS="$HOME/code/python/utils"
export PATH="$PYTHON_UTILS:$PATH"
Consider removing the .py from your file bc it is not needed in this case
Close and open your terminal, which is sourcing your file by its path
And now you should be able to treat your python file similar to a bash command
You don't need to use python3 filename.py to run the file, you can just use filename
From anywhere on your filesystem!
change alarm.py to include:
#!/bin/python
as the very first line in the file.
(or /usr/bin/python, depending on where you python interpreter is located. You can figure this out by typing: which python in the terminal.)
You can then just run alarm.py instead of python alarm.py.
e.g.:
~ toby$ alarm.py
And phihag who beat me by a few seconds is right, you need to add execute permissions (via chmod) to alarm.py.
You need to modify the Python specific path variable: PYTHONPATH.
So:
export PYTHONPATH=/Users/tobylieven/Documents/my_scripts
should get you working.
See: Python Module Search path
Something of interest that I really struggled with on OS X coming from Window, is that you its very hard to get the directory of your current script.
I found this.
#! /bin/zsh
cd "${0:h}"
Now you could execute a python file relative to the executed script instead of having to know the exact path where your python file is.
This might or might not help but I use this a lot to make my scripts and .command files work better.

Categories

Resources