Python3 file accessing [duplicate] - python

This question already has answers here:
How to read/process command line arguments?
(22 answers)
Closed last year.
Long story short I want to do a project but don't even know where to start or how to describe it for a google search.
When I run my python file in the Command Prompt I want to be able put a text file next to it that my program will read.
Like: D:> python3 name_of_file.py name_of_text_file.txt
Now when I execute the above I want my python file to somehow see the text file to do something with it (like outputting the contents) but I don't know how to do that. Any suggestions?
Now since I don't know where to start I do not have anything to show on what I have already tried.
Incase it helps: I am using Atom. & The python file and the text file are saved in the same folder.

Within a python script you can use the list sys.argv. The first list element (sys.argv[0]) is the name of the python script file (in your case "name_of_file.py"). The rest of the list elements are the provided command line arguments. So sys.argv[1] will contain the first command line argument.
import sys
# Retrieve first command line argument
file_name = sys.argv[1]
# Open text file
with open(file_name) as file:
# Read out contents of text file
file_text = file.read()
print(file_text)

Related

Type Text Into Terminal From Text File Using Python

I am new to python. I want to write a script that types in the first line of a text file into a terminal and then the second and so on. I have 2 questions. My first question is what command should I use to type into the terminal? I have not been able to find the answer to this. My other question is how can I input the first line of the text file, and then use the next line the next time through and so on? Sorry if these questions are to simple.

How to execute a command for each lines in a txt file [duplicate]

This question already has answers here:
How do I execute a program or call a system command?
(65 answers)
Closed 5 years ago.
i'm trying to automate the research about a list of domain i have (this list is a .txt file, about 350/400 lines).
I need to give the same command (that uses a py script) for each line i have in the txt file. Something like that:
import os
with open('/home/dogher/Desktop/copia.txt') as f:
for line in f:
process(line)
os.system("/home/dogher/Desktop/theHarvester-master/theHarvester.py -d "(line)" -l 300 -b google -f "(line)".html")
I know there is wrong syntax with "os.system" but i don't know how to insert the text in the line, into the command..
Thanks so much and sorry for bad english..
import os
with open('data.txt') as f:
for line in f:
os.system('python other.py ' + line)
If the contents of other.py are as follows:
import sys
print sys.argv[1]
then the output of the first code snippet would be the contents of your data.txt.
I hope this was what you wanted, instead of simply printing by print, you can process your line too.
Due to the Linux tag i suggest you a way to do what you want using bash
process_file.sh:
#!/bin/bash
#your input file
input=my_file.txt
#your python script
py_script=script.py
# use each line of the file `input` as argument of your script
while read line
do
python $py_script $line
done < "$input"
you can access the passed lines in python as follow:
script.py:
import sys
print sys.argv[1]
Hope below solution will be helpful for you :
with open('name.txt') as fp:
for line in fp:
subprocess.check_output('python name.py {}'.format(line), shell=True)
Sample File I have used :
name.py
import sys
name = sys.argv[1]
print name
name.txt:
harry
kat
patrick
Your approach subjects each line of your file to evaluation by the shell, which will break when (not if) it comes across a line with any of the characters with special meaning to the shell: spaces, quotes, parentheses, ampersands, semicolons, etc. Even if today's input file doesn't contain any such character, your next project will. So learn to do this correctly today:
for line in openfile:
subprocess.call("/home/dogher/Desktop/theHarvester-master/theHarvester.py",
"-d", line, "-l", "300", "-b", "google", "-f", line+".html")
Since the command line arguments do not need to be parsed, subprocess will execute your command without involving a shell.

How to execute a python file using txt file as input (to parse data)

I tried looking inside stackoverflow and other sources, but could not find the solution.
I am trying to run/execute a Python script (that parses the data) using a text file as input.
How do I go about doing it?
Thanks in advance.
These basics can be found using google :)
http://pythoncentral.io/execute-python-script-file-shell/
http://www.python-course.eu/python3_execute_script.php
Since you are new to Python make sure that you read Python For Beginners
Sample code Read.py:
import sys
with open(sys.argv[1], 'r') as f:
contents = f.read()
print contents
To execute this program in Windows:
C:\Users\Username\Desktop>python Read.py sample.txt
You can try saving the input in the desired format (line-wise) file, and then using it as an alternative to STDIN (standard input file) using the file subcommand with Python
python source.py file input.txt
Note: To use it with input or source files in any other directory, use complete file location instead of file names.

How to run a bat file from Python 3.3.2 [duplicate]

This question already has answers here:
Run a .bat file using python code
(9 answers)
Closed 8 years ago.
I am developing a program in python, however I have a little problem because the data that my program needs, it is from the result of a bat file, so I would like to create something that I help me to run just my script in python and not use the bat file directly.
Well, right now, I have to execute the following steps:
I create a txt file where I enter the input data (For example: A.txt)
I run the BAT file using the file A.txt
It creates a new file txt which has the result of running the BAT file (For example: B.txt)
I open the file B.txt and I copy all data and then I paste it in a new txt file, where it will serve as input data to my program in python
I run my program in python
it creates a new file txt where has the final results
End
How can I improve this, I mean, Which scripts I need to run my program in Python without to go to the Bat file and to do the steps that I describe above?
I'm pretty sure whatever you're doing in the bat file you can doit in a python module. So:
Write a python module sustitute for your *.bat
Write a python module containing a function for read the file A.txt and return the results. Then use that module from your program.
For instance:
# calculations.py, the bat substitute.
def calculate_from_input(file_name):
input_ = open(file_name, 'rb') # The mode you read the fike depends on your purposes.
# .... Do your calculations
return result # Return a list of velocities, for example.
Then in your main program
from calculations import calculate_from_input
calculated_data = calculate_from_input("A.txt") # Then, you have the data ready to use in your program.

Python file to open a text file and run other python files in the text file

I have a text file inside which I have paths to a few python files and the arguments that I would specify when I run them in a command prompt.
I am looking for a python script that opens up the text file and runs the python programs specified in the text file along with the provided arguments.
The text file will look something like
`C:\hello.py world
C:\square.py 5`
i think you should refer to below :
Calling an external command in Python
Step One
read all lines in your command file get a list of python script file name and arguments
like: " C:\hello.py and argument: word "
Step Two
call them in below code style
from subprocess import call
call(["python C:\hello.py", "word"])
......
I don't think this post deserves down voting. But from now on I would suggest to OP to look for a solution yourself, and then if you can't find the answer post on stack overflow!
from subprocess import call
with open("somefile.txt", 'r') as f:
some_files_to_run = [line.split('\n')[0] for line in f.readlines()]
for file_to_run in some_files_to_run:
call(["python", file_to_run])

Categories

Resources