【问题标题】:I am unable to check the files available in the directory我无法检查目录中可用的文件
【发布时间】:2020-11-01 20:04:57
【问题描述】:

我正在尝试读取当前目录中的 csv 文件。为此,我想检查当前目录中存在的所有文件。我试过用 check_output 函数来做。但是,我收到了这个错误,我无法弄清楚如何处理它。这是我尝试过的代码:

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

这是我收到的错误:

FileNotFoundError                         Traceback (most recent call last)
<ipython-input-15-c18143c64098> in <module>
      1 from subprocess import check_output
----> 2 print(check_output(["ls","../input"]).decode('utf8'))

~\Anaconda3\lib\subprocess.py in check_output(timeout, *popenargs, **kwargs)
    393 
    394     return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
--> 395                **kwargs).stdout
    396 
    397 

~\Anaconda3\lib\subprocess.py in run(input, capture_output, timeout, check, *popenargs, **kwargs)
    470         kwargs['stderr'] = PIPE
    471 
--> 472     with Popen(*popenargs, **kwargs) as process:
    473         try:
    474             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)
    773                                 c2pread, c2pwrite,
    774                                 errread, errwrite,
--> 775                                 restore_signals, start_new_session)
    776         except:
    777             # 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)
   1176                                          env,
   1177                                          os.fspath(cwd) if cwd is not None else None,
-> 1178                                          startupinfo)
   1179             finally:
   1180                 # Child is launched. Close the parent's copy of those pipe

FileNotFoundError: [WinError 2] The system cannot find the file specified

【问题讨论】:

  • os.listdir('../input')"../input" 不是current directory,也许这是您的问题 - 您在错误的文件夹中搜索。
  • 可能先检查..,看看..中是否有input
  • 您的错误表明您使用的是 Windows,如果 Windows 有命令 ls 或只有 dir,我不记得了。最好使用os.listdir()

标签: python unix anaconda subprocess data-science


【解决方案1】:

您可以通过以下方式获取当前目录中所有文件的列表:

import os
files = os.listdir('./')

【讨论】:

  • OP 问题不是如何列出文件位为什么他的代码不能正常工作。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-07
  • 1970-01-01
  • 2017-10-30
  • 1970-01-01
  • 2019-12-17
  • 1970-01-01
相关资源
最近更新 更多