This question already has answers here:
How do I create variable variables?
(17 answers)
Closed 4 years ago.
I am fairly new to Python and trying to figure out way to use variables from file as described below.
I have a file query.txt
query1="select count(*) from table1;"
query2="select count(*) from table2;"
My main program:
conn=connect_db()
print >>log,"connection successful"
c=conn.cursor()
with open('query.txt') as fp:
for line in fp:
print line
i=1
query="query"+str(i) #If I print query I get query1
#I am looking to pass query1 as argument, to execute first query
c.execute(query);
r=c.fetchone()
print r
i+=1
In shell I would use c.execute($query) and it would replace it with it's assigned value. How do I achieve it with Python?
Appreciate your help and guidance.
Change query.txt to:
select count(*) from table1;
select count(*) from table2;
Then in Python:
for query in fp:
c.execute(query)
You can use the sys library for that. https://docs.python.org/2/library/sys.html?highlight=argv#sys.argv
The list of command line arguments passed to a Python script. argv[0] is the script name (it is operating system dependent whether this is a full pathname or not). If the command was executed using the -c command line option to the interpreter, argv[0] is set to the string '-c'. If no script name was passed to the Python interpreter, argv[0] is the empty string.
To loop over the standard input, or the list of files given on the command line, see the fileinput module.
Argv allows you to pass parameters from the commandline when running your script. A tutorial can be found here:
https://www.tutorialspoint.com/python/python_command_line_arguments.htm
[EDIT]
2 Assumptions:
You are working with python 2.7
Your script runs commandline in
Linux.
Not saying it won't work otherwise, but this is what I know works.
[EDIT2]
Alex Hall's answer is actually what the OP needed, so focus on that one instead.
Related
This question already has answers here:
Tab completion in Python's raw_input()
(4 answers)
Closed 4 years ago.
I have made a python script that asks the user to enter 2 or 3 filenames, which should be analysed. The filename insertion is done in the script (it is not passed as argument to argparse, because there are other choices that user has to make before that). Due to the company naming convention these filenames can be quite long and thus cumbersome to type. To help the user I am printing the contents of directory. For now I am using something like this:
fname = raw_input("Insert phase 1 filename: ")
(than I check the if file exists etc...)
Is there a way to implement autocomplete for filenames inside the python script by writing custom input() function?
Note that the script must run on different machines / OS and I can not ask users to install some non-standard python libraries.
If there is no clean way to do it I might use less fancy solution of just printing a number before the filenames in the directory and than ask the user to insert just the number.
This might help:
Tab completion in Python's raw_input()
If you don't want to use that and just "guess" the file:
commands = ["cute_file", "awesome_file"]
def find_file(text):
options = [i for i in commands if i.startswith(text)]
if len(options):
return options
else:
return None
my_input = input("File name:")
print(find_file(my_input))
I'm using Ubuntu and have been trying to do a simple automation that requires me to input the [name of website] and the [file path] onto a list of command lines. I'm using subprocess and call function. I tried something simpler first using the "ls" command.
from subprocess import call
text = raw_input("> ")
("ls", "%s") % (text)
These returned as "buffsize must be an integer". I tried to found out what it was and apparently I had to pass the command as a list. So I tried doing it on the main thing im trying to code.
from subprocess import call
file_path = raw_input("> ")
site_name = raw_input("> ")
call("thug", -FZM -W "%s" -n "%s") % (site_name, file_path)
These passed as an invalid syntax on the first "%s". Can anyone point me to the correct direction?
You cannot use % on a tuple.
("ls", "%s") % text # Broken
You probably mean
("ls", "%s" % text)
But just "%s" % string is obviously going to return simply string, so there is no need to use formatting here.
("ls", text)
This still does nothing useful; did you forget the call?
You also cannot have unquoted strings in the argument to call.
call("thug", -FZM -W "%s" -n "%s") % (site_name, file_path) # broken
needs to have -FZM and -W quoted, and again, if you use format strings, the formatting needs to happen adjacent to the format string.
call(["thug", "-FZM", "-W", site_name, "-n", file_path])
Notice also how the first argument to call() is either a proper list, or a long single string (in which case you need shell=True, which you want to avoid if you can).
If you are writing new scripts, you most definitely should be thinking seriously about targetting Python 3 (in which case you want to pivot to subprocess.run() and input() instead of raw_input() too). Python 2 is already past its originally announced end-of-life date, though it was pushed back a few years because Py3k adoption was still slow a few years ago. It no longer is, and shouldn't be -- you want to be in Py3, that's where the future is.
Here is a complete example of how you would call a executable python file with subprocess.call Using argparse to properly parse the input.
Your python file to be called (sandboxArgParse.py):
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--filePath", help="Just A test", dest='filePath')
parser.add_argument("--siteName", help="Just A test", dest='siteName')
args = parser.parse_args()
print args.siteName
print args.filePath
Your calling python file:
from subprocess import call
call(["python","/users/dev/python/sandboxArgParse.py", "--filePath", "abcd.txt", "--siteName", "www.google.com"])
This question already has answers here:
how to direct output into a txt file in python in windows
(6 answers)
Closed 6 years ago.
I am running a python script which checks for the modifications of files in a folder. I want that output to be printed in a file. The problem is that the output is DYNAMIC , the cmd is always open and when a file is modified, I will have an information right-ahead about that in the cmd window. All the solutions which I found were matching the situations were I just run a command and I finish with that.
I tryed with:
python script.py > d:\output.txt but the output.txt file is empty
An example of the command prompt windows, after I run the command python script.py and I touch the 2 files, the command prompt will look like this. I want to capture that output.
Solution: In the python script which I use, add to the logging.basicConfig function, one more argument : filename='d:\test.log'
The issue is output buffering. If you wait long enough, you'll eventually see data show up in the file in "blocks". There are a few ways around it, for example:
Run python with the -u (unbuffered) flag
Add a sys.stdout.flush() after all print statements (which can be simplified by replacing stdout with a custom class to do it for you; see the linked question for more)
Add flush=True option to print statements if your version of Python supports it
If appropriate, use the logging module instead of print statements.
python test.py>test.txt
It's working for me in windows cmd prompt
As I see it the simplest would be to add the file handling (the writing to output.txt ) inside your script. Thus, when it is time to print the information you need to have (as your example shows when you touch two files you print two lines), you can open the file, write the specific line and close it after it is done (then you can see the updated output.txt).
Get the file path for the output.txt as a command line argument like
python script.py --o 'd:\output.txt'
for example.
This question already has answers here:
Python command line 'file input stream'
(3 answers)
Closed 8 years ago.
Is it possible to run a python script and feed in a file as an argument using <? For example, my script works as intended using the following command python scriptname.py input.txt and the following code stuffFile = open(sys.argv[1], 'r').
However, what I'm looking to do, if possible, is use this command line syntax: python scriptname.py < input.txt. Right now, running that command gives me only one argument, so I likely have to adjust my code in my script, but am not sure exactly how.
I have an automated system processing this command, so it needs to be exact. If that's possible with a Python script, I'd greatly appreciate some help!
< file is handled by the shell: the file doesn't get passed as an argument. Instead it becomes the standard input of your program, i.e., sys.stdin.
When you use the < operator in a shell you are actually opening the file and adding its contents to your scripts stdin
However there is is a python module that can do both. It's called fileinput.
https://docs.python.org/2/library/fileinput.html
It was shown in this post
How do you read from stdin in Python?
You can use the sys module's stdin attribute as a file like object.
This question already has answers here:
How to insert a variable value in a string in python
(3 answers)
Closed 2 years ago.
I'm creating a small script and I'd like to pass a varable into a bash command using the python programming language, so for example:
number = raw_input("digit: ")
then i'd like to take the number variable and put it in bash command so for example:
ssh 'foo%s.bar'(number) <- where the %s is located id like it be replaced with the input
Finally I'd like to take that and run it in a bash command still within the python script:
ssh foo45.bar
How can I make this work?
import subprocess
number = raw_input("digit: ")
subprocess.call(('ssh', 'foo{}.bar'.format(number)))
For some good reading, try the python docs for string formatting and for subprocess.
If you want to integrate the above with sshpass, replace the subprocess call with:
subprocess.call(('sshpass', '-p', 'YOUR_PASSWORD', 'ssh', '-o', 'StrictHostKeyChecking=no', 'foo{}.bar'.format(number)))