12

I've been using Jupyter Notebook to learn Principal Component Analysis from kaggle), but when I run this code

     from subprocess import check_output
     print(check_output(["ls", "../input"]).decode("utf8"))

I got an error below

FileNotFoundError Traceback (most recent call last)
<ipython-input-3-de0e39ca3ab8> in <module>()
      1 from subprocess import check_output
----> 2 print(check_output(["ls", "C:/Users/wanglei/Documents/input"]).decode("utf8"))

D:\Anaconda3\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs)
    624 
    625     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 626                **kwargs).stdout
    627 
    628 

D:\Anaconda3\lib\subprocess.py in run(input, timeout, check, *popenargs, **kwargs)
    691         kwargs['stdin'] = PIPE
    692 
--> 693     with Popen(*popenargs, **kwargs) as process:
    694         try:
    695             stdout, stderr = process.communicate(input, timeout=timeout)

D:\Anaconda3\lib\subprocess.py in __init__(self, args, bufsize, executable, stdin, stdout, stderr, preexec_fn, close_fds, shell, cwd, env, universal_newlines, startupinfo, creationflags, restore_signals, start_new_session, pass_fds)
    945                                 c2pread, c2pwrite,
    946                                 errread, errwrite,
--> 947                                 restore_signals, start_new_session)
    948         except:
    949             # Cleanup if the child failed starting.

D:\Anaconda3\lib\subprocess.py in _execute_child(self, args, executable, preexec_fn, close_fds, pass_fds, cwd, env, startupinfo, creationflags, shell, p2cread, p2cwrite, c2pread, c2pwrite, errread, errwrite, unused_restore_signals, unused_start_new_session)
   1222                                          env,
   1223                                          cwd,
-> 1224                                          startupinfo)
   1225             finally:
   1226                 # Child is launched. Close the parent's copy of those pipe

The path is correct, and it seems that all the call from subprocess will fail similarly.

Does anyone have any idea why this is happening?

phihag
  • 278,196
  • 72
  • 453
  • 469
learing
  • 123
  • 1
  • 1
  • 5

3 Answers3

11

This code runs the ls command, which is available on all POSIX-conforming systems.

You are using Microsoft Windows. Microsoft Windows does not conform to POSIX by default. For instance, there is no ls binary. Therefore, subprocess cannot find the file ls, and thus emits a FileNotFoundError.

You can install Microsoft's Bash on Windows, which will give you ls.

However, the pythonic and more portable way to list a directory is not to use subprocess in the first place, but the built-in os.listdir:

import os
print(os.listdir('../input'))
phihag
  • 278,196
  • 72
  • 453
  • 469
  • Yes, you make a good point,and this works,but could you analyze why the subprocess module didn't work?Thx – learing Mar 29 '17 at 09:06
  • The subprocess module works fine. There is just no `ls` binary on your filesystem. Therefore, subprocess cannot find the `ls` file, and thus emits an `FileNotFoundError`. – phihag Mar 29 '17 at 09:07
  • 1
    Yes,I think you are right,all the example I run came from official website was baesd on POSIX,that's why the call all failed,bacause the Windows just do not follow the POSIX. LOL – learing Mar 29 '17 at 09:20
  • We can just use, print(os.listdir('input')) , if you are using os package. – Karan Sep 19 '18 at 03:13
0

I happened to have the same question. Using the below codes provided by Phihag works good for me on Jupiter Notebook on Windows. Thank you.

    import os
    print(os.listdir('../input'))
Emma Zhang
  • 101
  • 1
  • 2
  • 4
-1

try specifying the working directory by adding a cwd arguement, this should work

import os, subprocess
cwd = os.getcwd()
proc = subprocess.Popen(["ls" ,"../input"], cwd=cwd, stdout=subprocess.PIPE)
output = proc.stdout.read().decode("utf8")
Abdallah Alsamman
  • 1,623
  • 14
  • 22
  • 2
    That is already the default for `cwd`. The problem here is also not related to the argument of `ls` (if it were, there would not be a `FileNotFound` error, but an error message from ls), but the binary itself. – phihag Mar 29 '17 at 08:49
  • unfortunately,it throws the same error.could this be the problem of subprocess module? – learing Mar 29 '17 at 09:01