1

I am trying to find the file type using file with subprocess

    cwdir = os.getcwd()
    Fileinput=cwdir+"/"+'testfile.zip'
    print "Current Directory %s"% cwdir
    Fileformat=subprocess.Popen('file' + Fileinput)

I get OSError: [Errno 2] No such file or directory. I verified and the file does exist in the path. Thanks for any help with this.

user1719051
  • 109
  • 2
  • 14

1 Answers1

2

Add space between 'file' and fileinput

Fileformat = subprocess.Popen('file ' + Fileinput)
#                                  ^

Otherwise, file/current/path/testfile.zip is treated as executable path instead of file.

Or use following form:

Fileformat = subprocess.Popen(['file', Fileinput])

you have to pass stdout=subprocess.PIPE to Popen and read using Fileformat.stdout.read() if you want get output of the command.

How about using subprocess.check_output?

>>> subprocess.check_output(['file', '/etc/passwd'])
'/etc/passwd: ASCII text\n'
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • I still get the same error even after adding the space for 'file '. However I changed it to check_output and it works. Thanks. – user1719051 Oct 28 '13 at 05:33