Kaggle學習:FileNotFoundError: [WinError 2] 系統找不到指定的檔案
阿新 • • 發佈:2018-12-13
使用Jupyter notebook時執行此段程式碼:
from subprocess import check_output
print(check_output(["ls", "C:/Kaggle/Titanic/input"]).decode("utf8"))
出現錯誤:
FileNotFoundError Traceback (most recent call last) <ipython-input-10-cf7fc002cdae> in <module>() 32 # input directory 33 from subprocess import check_output ---> 34 print(check_output(["ls", "C:/Kaggle/Titanic/input"]).decode("utf8")) 35 #import os 36 #print(os.listdir('C:/Kaggle/Titanic/input')) ~\Anaconda3\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs) 374 375 return run(*popenargs, stdout=PIPE, timeout=timeout, check=True, --> 376 **kwargs).stdout 377 378 ~\Anaconda3\lib\subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs) 451 kwargs['stderr'] = PIPE 452 --> 453 with Popen(*popenargs, **kwargs) as process: 454 try: 455 stdout, stderr = process.communicate(input, timeout=timeout) ~\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, encoding, errors, text) 754 c2pread, c2pwrite, 755 errread, errwrite, --> 756 restore_signals, start_new_session) 757 except: 758 # Cleanup if the child failed starting. ~\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) 1153 env, 1154 os.fspath(cwd) if cwd is not None else None, -> 1155 startupinfo) 1156 finally: 1157 # Child is launched. Close the parent's copy of those pipe FileNotFoundError: [WinError 2] 系統找不到指定的檔案。
- 原因
因為使用的是Windows,預設情況下,Windows不符合POSIX。例如,沒有ls二進位制檔案。因此,子程序無法找到該檔案ls,從而發出一個FileNotFoundError。
- 解決辦法
- 列出目錄的更便攜的方法是使用os.listdir:
import os
print(os.listdir('C:/Kaggle/Titanic/input'))
結果展示:[‘gender_submission.csv’, ‘test.csv’, ‘train.csv’]