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

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.

Related

Python3 file accessing [duplicate]

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)

Cannot access one .txt file with two scripts

Here is my problem :
I got a python program that writes into a file, and a batch one that reads the file then. So I got my python code (simplified) :
import os
file = open("test.txt", "w")
file.write(Chooseentry.get()+"\n")
for i in hal:
file.write(i+"\n")
file.close()
os.startfile (r"C:/Hack/Project/Read.bat")
So I start my batch after having closed the file. It is supposed not to be used anymore by python, right ?
But then my batch file that reads it tells me :
The system cannot find the file test.txt
The process cannot access to the file because this file is already used by another process
Soooo... How can I close properly my file in python so that I can use it with my batch then ?
Thanks by advance for any help ! ^^

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.

Python reading a file line by line and sending it to another Python script

Good day.
Today i was trying to practice python and Im trying to make a script that reads the lines from a file containing only numbers and using said numbers as a parameter in another Python script.
Here at work i sometimes need to execute a python script called Suspend.py, ever time i excute this scripit i must type the following information:
Suspend.py suspend telefoneNumber
I have to do this procedure many times during the day and i have to do this for every number on the list, it is usually a very long list. SO i though on trying to make things a little bit faster and creat a Python script myself.
Thing is a just started learning Python on my own i kinda suck at it, so i have no idea on how to do this.
In one file i have the following numbers:
87475899
87727856
87781681
87794922
87824499
88063188
88179211
88196532
88244043
88280924
88319531
88421427
88491113
I want python to be able to read line by line and send this number to another file script together with the word "suspend" on the previously said python script.
If I understand you correctly:
import subprocess
with open("file_with_numbers.txt") as f:
for line in f:
subprocess.call(["python", "Suspend.py", "suspend", line.strip()])

Capture dynamic command prompt output in text file [duplicate]

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.

Categories

Resources