I have a script.py in /Users/admin/Desktop and I want to run this script on a file that is in /Users/admin//Desktop/folder/file.txt, without changing the present dir.
Question: what is the most efficient way to do that on command-line ? I am using the following commands and results are not as expected.
$ python script.py --script; /Users/admin/Desktop/file.txt
raise StopIteration('because of missing file (file.txt)')
StopIteration: because of missing file (file.txt)
Remove the semicolon because that will prematurely terminate the command.
Pass the correct path to the file to your program. You say it is /Users/admin/Desktop/folder/file.txt, however, your command is using /Users/admin/Desktop/file.txt (it's missing folder)
So the command should (probably) be:
$ python script.py --script /Users/admin/Desktop/folder/file.txt
If that doesn't work you will need to edit your question to show your code.
Related
I want to run a python script foo.py from the command line like this
$ foo
Using a shebang in foo.py, for example:
#!/usr/bin/env python
print('this is foo')
allows me to call it like this:
$ ./foo.py
How do I drop the leading ./ and the trailing .py?
First, rename the file from foo.py to foo.
Then, move the file to /usr/local/bin/ or /home/user/.local/bin if the script will only be executed by a single user. Instead, if your script is placed somewhere in the system for example "/path/to/foo", you could add your "/path/to/foo" to the $PATH variable.
After opening a new terminal session. You should be able to execute the script without the "./" and ".py".
By the way "./" means that you want to execute a file in the current working directory. It is always possible to execute a file using a full path of the file, for example "/usr/bin/something_to_run".
Please consider reading about PATH variable here.
I'm a COMPLETE beginner to python, I'm making my first python script that really does anything. I'm assigning a directory to a variable so I can use it to move a file, but the directory includes a folder starting with . and python says it's invalid syntax. So how can I get python to ignore the .?
EDIT: Here's the code
#!/usr/bin/env python
import os
Optifine = find /home/Sol33t303/.minecraft -name
OptiFine_1.10.2_HD_U_E3.jar
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
You are mixing two very different things.
Are you writing Python or Bash, cause this is totally Bash:
Optifine = find /home/Sol33t303/.minecraft -name
You can't just run Bash commands inside a Python script!
If, for example you want to run a shell command inside your script and get its output you can use:
Optifine = subprocess.check_output(['find', '/home/Sol33t303/.minecraft', '-name'])
And then you split the output by line, and foreach line (file found), move it to the desired destination:
for line in Optifine.splitlines():
shutil.move(Optifine, "/home/Sol33t303/.minecraft/mods")
I have a self-installed python in my user directory in a corporate UNIX SUSE computer (no sudo privilege):
which python
<user>/bin/python/Python-3.6.1/python
I have an executable (chmod 777) sample.py file with this line at the top of the file:
#!<user>/bin/python/Python-3.6.1/python
I can execute the file like this:
python sample.py
But when I run it by itself I get an error:
/full/path/sample.py
/full/path/sample.py: Command not found
I have no idea why it's not working. I'm discombobulated as what might be going wrong since the file is executable, the python path is correct, and the file executes if I put a python command in the front. What am I missing?
EDIT:
I tried putting this on top of the file:
#!/usr/bin/env python
Now, I get this error:
: No such file or directory
I tried this to make sure my env is correct
which env
/usr/bin/env
EDIT2:
Yes, I can run the script fine using the shebang command like this:
<user>/bin/python/Python-3.6.1/python /full/path/sample.py
Your file has DOS line endings (CR+LF). It works if you run python sample.py but doesn't work if you run ./sample.py. Recode the file so it has Unix line endings (pure LF at the end of every line).
Try using #!/usr/bin/env python as described in this post. Let the OS do the work.
In brief
I can't run a simple Python file with +x permission set and shebang line.
In Details
Let's take a simple Python code in myApp.py file at some $CODE_HOME folder
#!/usr/bin/python
print 122
When cd $CODE_HOME and running this file from console
. ./myApp.py
I got error as
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /usr/bin/print line 528.
Error: no such file "122333"
Though running by python myApp.py will get thing work.
The question
What's wrong is that? How to fix it?
. myApp.py is an instruction to Bash to source the passed file, ie execute it within the current process.
To execute a script or other file, you need to reference it by path:
./myApp.py (or just python myApp.py)
i.e. omitting the starting '.' in your call
To answer your question as is, . is the source command, which just runs each of the commands in the argument script in the context of the calling terminal. In your case, this doesn't do anything for the first line, then tries to call print, as you can see in
at /usr/bin/print line 528
Use ./myApp.py instead.
im kinda new to the python world and im having some issues running a bash file that will be automatically from my python script (using linux) .
i set my python script to create both a text file .geo and a Bash file .sh in a directory somewhere in my Desktop like this :
basedirectory="/home/pst2/Desktop/";
*//Writing the .geo file*
file = open(basedirectory+nomdossier+"/"+nomfichier+".geo", 'w');
file.write
..blabla
..blabla
file.close();
//Writing the .sh file
file = open(basedirectory+nomdossier+"/"+nomfichier+".sh", 'w');
file.write
..blabla
..blabla
file.close();
Now at this point my script works perfectly with all the variables set up and working fine and both those files that i created find themselves in this directory (for exemple after running the python script and entering the variables)
/home/pst2/Desktop/test/
(and in here you will find the new test.geo and test.sh that were created via the python script)
basically the test.sh when executed "manually" with Bash test.sh ( whenever i am in its directory on ubuntu) will create another file called test.msh in the same directory
and i cant seem to find the right coding , using the subprocess modules to execute the newly created test.sh file automatically from the script .
is there a way to do so , like with indicating the absolute path to the .sh file
(in our case basedirectory+nomdossier+"/"+nomfichier+".sh ) ?
take a look at the os module.
I believe
os.system("command_line_with_args")
could be what your looking for
Not sure what you are writing into the .sh file.
But to start with:
Have you started your .sh-file with the hashbang? #!/bin/sh
Have you modded your file as executable? chmod +x
After you have done this, you should be able to use subprocess module and do something like the example from the manual for subprocess:
subprocess.call([path_to_script+'/script.sh'])
I might have to update this answer if & when new information comes to my attention
Roughly equivalent to "manually" executing bash test.sh with the current directory being the one where test.sh has been written by your posted code is:
from subprocess import call
call(['bash', 'test.sh'], cwd=basedirectory+nomdossier)