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 in Python by Jasmin
57 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 by Tamanna

Related Questions In Python

0 votes
1 answer

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,520 points
9,580 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 in Python by Alisha
131 views
0 votes
0 answers

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

I get the following error while executing ...READ MORE

Jun 13 in Python by Kamal

edited Jun 13 75 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 in Python by Vinayak
111 views
+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 6 in Python by Neha
• 330 points

edited Jul 8 by Kalgi 175 views
+4 votes
6 answers
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
• 67,290 points
1,172 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 in Python by Omkar
• 67,290 points
25 views