Passing stdin for cpp program using a Python script - c++

I'm trying to write a python script which
1) Compiles a cpp file.
2) Reads a text file "Input.txt" which has to be fed to the cpp file.
3) Compare the output with "Output.txt" file and Print "Pass" if all test cases have passed successfully else print "Fail".
`
import subprocess
from subprocess import PIPE
from subprocess import Popen
subprocess.call(["g++", "eg.cpp"])
inputFile = open("input.txt",'r')
s = inputFile.readlines()
for i in s :
proc = Popen("./a.out", stdin=int(i), stdout=PIPE)
out = proc.communicate()
print(out)
`
For the above code, I'm getting an output like this,
(b'32769', None)
(b'32767', None)
(b'32768', None)
Traceback (most recent call last):
File "/home/zanark/PycharmProjects/TestCase/subprocessEg.py", line 23, in <module>
proc = Popen("./a.out", stdin=int(i), stdout=PIPE)
ValueError: invalid literal for int() with base 10: '\n'
PS :- eg.cpp contains code to increment the number from the "Input.txt" by 2.

pass the string to communicate instead, and open your file as binary (else python 3 won't like it / you'll have to encode your string as bytes):
with open("input.txt",'rb') as input_file:
for i in input_file:
print("Feeding {} to program".format(i.decode("ascii").strip())
proc = Popen("./a.out", stdin=PIPE, stdout=PIPE)
out,err = proc.communicate(input=i)
print(out)
also don't convert input of the file to integer. Leave it as string (I suspect you'll have to filter out blank lines, though)

Related

Getting TypeError trying to open() a file in write mode with Python

I have a Python script that in my mind should:
Open a file
Save its content in a variable
For each line in the variable:
Edit it with a regular expression
Append it to another variable
Write the second variable to the original file
Here is a MWE version of the script:
# [omitting some setup]
with open(setFile, 'r') as setFile:
olddata = setFile.readlines()
newdata = ''
for line in olddata:
newdata += re.sub(regex, newset, line)
with open(setFile, 'w') as setFile:
setFile.write(newdata)
When I run the script I get the following error:
Traceback (most recent call last):
File C:\myFolder\myScript.py, line 11, in <module>
with open(setFile, 'w') as setFile:
TypeError: expected str, bytes or os.PathLike object, not _io.TextIOWrapper
As far as I can understand, Python is complaining about receiving the setFile variable as an argument of open() because it isn't of the expected type, but then why did it accept it before (when I only read the file)?
I suppose that my mistake is quite evident but, as I am a neophyte in Python, I can't find out where it is. Could anyone give me a help?
just curious why you are using the same variable name for your file and then as your filehandler and then again in your next with function.
_io.TextIOWrapper is the object from your previous open, which has been asssigned to the setFile variable.
try:
with open(setFile, 'r') as readFile:
olddata = readFile.readlines()
newdata = ''
for line in olddata:
newdata += re.sub(regex, newset, line)
with open(setFile, 'w') as writeFile:
writeFile.write(newdata)

Saving files in Python using "with" method

I am wanting to create a file and save it to json format. Every example I find specifies the 'open' method. I am using Python 2.7 on Windows. Please help me understand why the 'open' is necessary for a file I am saving for the first time.
I have read every tutorial I could find and researched this issue but with no luck still. I do not want to create the file outside of my program and then have my program overwrite it.
Here is my code:
def savefile():
filename = filedialog.asksaveasfilename(initialdir =
"./Documents/WorkingDirectory/",title = "Save file",filetypes = (("JSON
files","*.json"), ("All files", "*.")))
with open(filename, 'r+') as currentfile:
data = currentfile.read()
print (data)
Here is this error I get:
Exception in Tkinter callback Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1542, in call
return self.func(*args) File "C:\Users\CurrentUser\Desktop\newproject.py", line 174, in savefile
with open(filename, 'r+') as currentfile: IOError: [Errno 2] No such file or directory:
u'C:/Users/CurrentUser/Documents/WorkingDirectory/test.json'
Ok, I figured it out! The problem was the mode "r+". Since I am creating the file, there is no need for read and write, just write. So I changed the mode to 'w' and that fixed it. I also added the '.json' so it would be automatically added after the filename.
def savefile():
filename = filedialog.asksaveasfilename(initialdir =
"./Documents/WorkingDirectory/",title = "Save file",filetypes = (("JSON
files","*.json"), ("All files", "*.")))
with open(filename + ".json", 'w') as currentfile:
line1 = currentfile.write(stringone)
line2 = currentfile.write(stringtwo)
print (line1,line2)

Errno 22 when using shutil.copyfile on dictionary values in python

I am getting a feedback error message that I can't seem to resolve. I have a csv file that I am trying to read and generate pdf files based on the county they fall in. If there is only one map in that county then I do not need to append the files (code TBD once this hurdle is resolved as I am sure I will run into the same issue with the code when using pyPDF2) and want to simply copy the map to a new directory with a new name. The shutil.copyfile does not seem to recognize the path as valid for County3 which meets the condition to execute this command.
Map.csv file
County Maps
County1 C:\maps\map1.pdf
County1 C:\maps\map2.pdf
County2 C:\maps\map1.pdf
County2 C:\maps\map3.pdf
County3 C:\maps\map3.pdf
County4 C:\maps\map2.pdf
County4 C:\maps\map3.pdf
County4 C:\maps\map4.pdf
My code:
import csv, os
import shutil
from PyPDF2 import PdfFileMerger, PdfFileReader, PdfFileWriter
merged_file = PdfFileMerger()
counties = {}
with open(r'C:\maps\Maps.csv') as csvfile:
reader = csv.reader(csvfile, delimiter=",")
for n, row in enumerate(reader):
if not n:
continue
county, location = row
if county not in counties:
counties[county] = list()
counties[county].append((location))
for k, v in counties.items():
newPdfFile = ('C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(str(v).split(',')) > 1:
print newPdfFile
else:
shutil.copyfile(str(v),newPdfFile)
print 'v: ' + str(v)
Feedback message:
C:\maps\Maps\JoinedMaps\County-County4.pdf
C:\maps\Maps\JoinedMaps\County-County1.pdf
v: ['C:\\maps\\map3.pdf']
Traceback (most recent call last):
File "<module2>", line 22, in <module>
File "C:\Python27\ArcGIS10.5\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
There are no blank lines in the csv file. In the csv file I tried changing the back slashes to forward slashes, double slashes, etc. I still get the error message. Is it because data is returned in brackets? If so, how do I strip these?
You are actually trying to create the file ['C:\maps\map3.pdf'], you can tell this because the error messages shows the filename its trying to create:
IOError: [Errno 22] invalid mode ('rb') or filename: "['C:\\\\maps\\\\map3.pdf']"
This value comes from the fact that you are converting to string, the value of the dictionary key, which is a list here:
shutil.copyfile(str(v),newPdfFile)
What you need to do is check if the list has more than one member or not, then step through each member of the list (the v) and copy the file.
for k, v in counties.items():
newPdfFile = (r'C:\maps\Maps\JoinedMaps\County-' + k +'.pdf')
if len(v) > 1:
print newPdfFile
else:
for filename in v:
shutil.copyfile(filename, newPdfFile)
print('v: {}'.format(filename))

Reading a list of URLs into a Python list

I am a newbie in Python and I wish to read a list of URLs into a Python list. Below is my script :
if __name__ == 'main' :
urls_list = [] #Creating the URL vector
with open('url_list.doc') as my_file :
for line in my_file :
urls_list.append(line)
print (urls_list)
The above code does not generate any output or error even. Below is url_list.doc :
https://www.nytimes.com/2017/03/03/us/politics/majority-rule-means-the-power-to-stop-not-just-start-an-investigation.html
https://www.nytimes.com/2017/03/03/business/retirement/millennials-ask-elders-about-retirement.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=photo-spot-region&region=top-news&WT.nav=top-news
https://www.nytimes.com/2017/03/02/us/politics/jeff-sessions-russia-trump-investigation-democrats.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=first-column-region&region=top-news&WT.nav=top-news
https://www.nytimes.com/2017/03/02/business/trump-pentagon-budget.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=first-column-region&region=top-news&WT.nav=top-news
https://www.nytimes.com/2017/03/03/style/modern-love-you-may-want-to-marry-my-husband.html?hp&action=click&pgtype=Homepage&clickSource=story-heading&module=second-column-region&region=top-news&WT.nav=top-news
Any suggestions on how to perform this.
To parse .doc files you need Antiword. It is a standalone command line executable so we are going to have to use Popen & PIPE.
Code:
from subprocess import Popen, PIPE
file_path = "Your file path here"
cmd = ['antiword', file_path]
p = Popen(cmd, stdout=PIPE)
stdout, stderr = p.communicate()
text = stdout.decode('ascii', 'ignore')
text now has all the content of the .doc file.

Is it possible to use output from raw_input() as a new file name?

I'm wondering if it's possible to use the return from raw_input()to create a file name?
What I have so far:
import os
from Tkinter import Tk
from tkFileDialog import askopenfilename
Tk().withdraw()
ttnum=str(raw_input('Please enter ticket number: '))
ttnum
filename=askopenfilename()
abspath = os.path.abspath(filename)
dname = os.path.dirname(abspath)
os.chdir(dname)
f=open(filename)
contents=f.read()
file_len(filename)
file_scan(filename)
Portion of code that calls ttnum:
def file_len(filename):
#Count the number of line in the Text File
f1 = open(("WiFi Logs report for tt " + ttnum,'w'))
with open(filename) as f:
for i, l in enumerate(f):
pass
f1.write('Total number of lines in file: ' + str(i+1) + '\n' + '\n')
f1.close()
def file_scan(filename):
#List of issues to Scan For
f1 = open(("WiFi Logs report for tt " + ttnum,'a'))
I can enter input no problem (in this case 12345), but once it hits the code, I get the following:
Traceback (most recent call last):
File "M:\WiFi Log Scanner\WiFi_Log_Scanner.py", line 153, in <module>
file_len(filename)
File "M:\WiFi Log Scanner\WiFi_Log_Scanner.py", line 4, in file_len
f1 = open(("WiFi Logs report for tt " + ttnum,'w'))
TypeError: coercing to Unicode: need string or buffer, tuple found
I thought that the str() at the start would ensure that it is, well, a string and not a tuple?
Any insight would be appreciated.
Thanks,
Joe
Remove a layer of parentheses from open(("WiFi Logs report for tt " + ttnum,'a')):
open("WiFi Logs report for tt " + ttnum,'a')
With extra parentheses, you pass one argument to open, and this argument is a tuple: a pair of values, which is not what open expects for its first argument.
A side note (unrelated to your error): you don't have to chdir before you read a file (and your actual code only works when filename is already absolute, which the result of tk_getOpenFile is. chdir doesn't help anything here). And when chdir is necessary, it's error-prone (it introduces hidden state) and thread-unsafe.