Error saying ValueError Invalid file object io BufferedReader name 9

0 votes

I am trying to get a file name and pass it to a command using popen. Then I want to print the output. This is my code:

filePath = tkinter.filedialog.askopenfilename(filetypes=[("All files", "*.*")])

fileNameStringForm = (basename(filePath ))
fileNameByteForm = fileNameStringForm.encode(encoding='utf-8')

process = subprocess.Popen(['gagner','-arg1'], shell = True, stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
process .communicate(fileNameByteForm )

stdout, stderr = process .communicate()  <<------ERROR POINTS TO THIS LINE

stringOutput = stdout.decode('urf-8')
print(stringOutput)

I am getting the following error:

ValueError: Invalid file object: <_io.BufferedReader name=9>
May 28, 2019 in Python by Jasmin
5,591 views

1 answer to this question.

0 votes

Once you've executed the following command, your subprocess has finished and the pipes have been closed and hence the second call will fail

process.communicate(fileNameByteForm)

your subprocess has finished and the pipes have been closed. The second call will then fail as a result.

What you want to do instead is

stdout, stderr = process.communicate(input_data)

This will pipe your input data into the subprocess and read stdout and stderr.

answered May 28, 2019 by Tamanna

Related Questions In Python

+2 votes
10 answers

How to fix this? ValueError: invalid literal for int() with base 10 error in Python

The following are totally acceptable in python: passing ...READ MORE

answered Nov 16, 2018 in Python by Nymeria
• 3,560 points
409,221 views
0 votes
1 answer

Error saying "TypeError: descriptor object needs an argument"

The error is pretty straight forward, toy ...READ MORE

answered May 28, 2019 in Python by Alisha
13,536 views
–1 vote
1 answer

Python error saying "NameError: name 'email' is not defined"

you need to define the variable email READ MORE

answered Nov 29, 2019 in Python by Casper
• 160 points
6,836 views
0 votes
1 answer

Python Error saying "NameError: global name 'true' is not defined"

It's a very small mistake. Change true ...READ MORE

answered Jun 17, 2019 in Python by Vinayak

edited Oct 7, 2021 by Sarfaraz 16,195 views
0 votes
2 answers
+1 vote
2 answers

how can i count the items in a list?

Syntax :            list. count(value) Code: colors = ['red', 'green', ...READ MORE

answered Jul 7, 2019 in Python by Neha
• 330 points

edited Jul 8, 2019 by Kalgi 4,434 views
0 votes
1 answer
+5 votes
6 answers

Lowercase in Python

You can simply the built-in function in ...READ MORE

answered Apr 11, 2018 in Python by hemant
• 5,790 points
4,070 views
0 votes
1 answer

Python argparse error "NameError: name 'file' is not defined"

The right datatype for destination in argpasrse module ...READ MORE

answered Nov 28, 2018 in Python by Omkar
• 69,220 points
13,706 views
0 votes
1 answer

How to print a message or the error if a file is not found?

To print the message that file is not ...READ MORE

answered Jan 2, 2019 in Python by Omkar
• 69,220 points
2,867 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP