Print all print() in log file on exit - python-3.x

I have a script with a print in a while loop.
This loop is running for like 10 hours and print something every minute or so.
I would like to store all print() outputs in a single log file
I don't know how to proceed
I use windows and Python 3.6

print can send the output to a file. Just provide a file descriptor:
for i in range(10): # or a `while` loop
with open('mylog.log', 'a') as f:
print("h0i", file=f)

Related

Is there a way to save a file that is written by a python script, if the python script is killed before it is completed?

I have been running a web scraper script written in Python. I had to terminate the Python script because of an issue with my internet connection. At the time, the script has run for almost 2-3 hours. I used a for loop to write the data into a CSV file. I had used 'file.close()' to save the file once the for loop is over; but as I terminated the program early, my time of two hours have wasted.
Once I tried to delete the newly created CSV file(its size is 0kB), it is said 'The action can't be completed because the file is open in Python'. I thought that all the data I extracted is now on the RAM.(maybe that's why I don't get the permission to close the 0kB sized CSV file?)
So, is there any way to access those data and write the data into the above-mentioned CSV file? (Otherwise, I will have to run to the same program for another two hours and wait for the results)
Here's my code!
#! python3.8
fileCsv = open('newCsv.csv','w',newline='')
outputWriter = csv.writer(fileCsv)
for i in range(100,000): # whatever range
num, name = 10000, 'hello' # The data extracted from the website
ourputWriter.writerow([num,name])
time.sleep(1)
fileCsv.close() # My program was terminated before this line, in the for loop
Using with should help here.
with open('newCsv.csv','w') as wr:
for i in range(100,000): # whatever range
num, name = 10000, 'hello' # The data extracted from the website
wr.writerow([num,name])
time.sleep(1)

I am writing a script to scrape a website and download files. After downloading, I am trying to rename the files and it's not working

path='C:/Users/ks/Downloads/AU/ORR/'
print (path)
substring = "w"
files = os.listdir(path)
for file in files:
src=path+file
if substring in file:
break
print ("there")
else:
print ("not there")
dst='Pattern_'+substring+str(org)+str(m1)+'.csv'
dst=path+dst
os.rename(src,dst)
The print statements are just for debugging. Everything is getting printed but still the rename function is not working.
Using break leaves the loop for good. You probably want continue to skip exactly this iteration of the loop.
Your code will stop doing anything after the first time substring is not in file.
See break-and-continue-statements-and-else-clauses-on-loops

how to create a dynamic list to read and update for each script run

i have a script which runs some checks for all DB's.Now i want to have a list such that this list contains all the DB's already checked.So the next time the script runs it will read this list and if the DB is not in that list only then the checks happen.
What is the best way to implement this? If i initialize a empty list(DB_checked) and append each DB name while the checks are run,the issue would be that each time the script starts the list would again be empty.
Please suggest.Thanks.
At the end of the script will call the below function to write to disk:
def writeDBList(db_checked):
with open(Path(__file__).parent / "db_names.txt", "w") as fp:
for s in job_names:
fp.write(str(s) +"\n")
return
When the script starts will call the below to read the file from disk:
def readDBList():
with open(Path(__file__).parent / "db_names.txt", "r") as fp:
for line in fp:
db_list.append(line.strip())
return
But how to convert the file contents to a list so that i can easily check with below:
checked_list = readDBList()
if db not in checked_list:
....
....
You need to write this list to the disk after the script finishes the checks, and read it again in the beginning of the script in the next script run.
# Read DB CheckList
DB_List = readDBList()
# Your normal script functionality for only DBs not in the list
# Store DB CheckList
writeDBList(DB_List)
Check this in case you are not familiar with I/O file handling in python.
Now, regarding your second question about how to read the list. I would suggest using pickle, which allows you to read/write python structures without worrying about stringfying or parsing.
import pickle
def writeDBList():
with open('DBListFile', 'wb') as fp:
pickle.dump(DBList, fp)
def readDBList():
with open ('DBListFile', 'rb') as fp:
DBList= pickle.load(fp)

Pass a Python input parameter to a Batch File

Is it at all possible to build a Python GUI (lets say using Tkinter) and then pass the users input from the Python GUI into a windows batch file.
My objective is to make batch files have a nice front end using Python.
Simple example:
In the Python code the user will be asked for a date
date = inputInt("Please enter Date yyyymmdd")
Now I need to put this date value into a windows batchfile.
When running the the Python program you should use a pipe, to redirect it's stdout to stdin of the batch file. In the batch file you can just wait on the stdin until something is outputed by the Python program. Take a look here to see how to read an input stream in batch. It would look something like this:
python myprogram.py | batch_file.bat
I used the following code to send the data to a text file
import sys
startdate = input("Please Enter StartDate YYYYMMDD ")
orig_stdout = sys.stdout
f = open('startdate.txt', 'w')
sys.stdout = f
print(startdate)
sys.stdout = orig_stdout
f.close()
I then used the following in my batch file to read the text file contents
#echo off
set /p startdate=<startdate.txt
echo %startdate%

debugging a python script taking input from sys.stdin in pycharm

I want to debug a small python script that takes input from stdin and sends it to stdout. Used like this:
filter.py < in.txt > out.txt
There does not seem to be a way to configure Pycharm debugging to pipe input from my test data file.
This question has been asked before, and the answer has been, basically "you can't--rewrite the script to read from a file."
I modified the code to take a file, more or less doubling the code size, with this:
import argparse
if __name__ == '__main__':
cmd_parser = argparse.ArgumentParser()
cmd_parser.add_argument('path', nargs='?', default='/dev/stdin')
args = cmd_parser.parse_args()
with open(in_path) as f:
filter(f)
where filter() now takes a file object open for write as a parameter. This permits backward compatibility so it can be used as above, while I am also able to invoke it under the debugger with input from a file.
I consider this an ugly solution. Is there a cleaner alternative? Perhaps something that leaves the ugliness in a separate file?
If you want something simpler, you can forgo argparse entirely and just use the sys.argv list to get the first argument.
import sys
if len(sys.argv) > 1:
filename = sys.argv[1]
else:
filename = sys.stdin
with open(filename) as f:
filter(f)

Resources