【问题标题】:No such file or directory but all files in the same folder没有这样的文件或目录,但同一文件夹中的所有文件
【发布时间】:2021-11-13 18:19:46
【问题描述】:

我得到一个 No such file or directory: 'SnP1000_TICKR.csv' 错误,但我所有的文件都在以下文件夹中:

我在这里调用文件

在这段代码上运行的:

def finVizEngine(input,output):
    import chromedriver_autoinstaller
    chromedriver_autoinstaller.install()  # Check if the current version of chromedriver exists
                                      # and if it doesn't exist, download it automatically,
                                      # then add chromedriver to path

    driver = webdriver.Chrome()
    ipo_df = pd.DataFrame({})
    openFinViz()
    with open(input, 'r') as IPO_List:
        csv_reader = reader(IPO_List)

这是以前运行的,但后来我将文件上传到 Github 并开始从 vscode 而不是 pycharm 运行文件,并开始出现大量错误,但老实说不明白出了什么问题。任何帮助都会很棒,

最好的乔奥

【问题讨论】:

  • Python 不在乎脚本是什么目录。 Python 只关心你的current working directory,这可能是你项目的根。
  • 您如何准确地运行您的脚本?例如。 VS Code 调试器,VS Code 集成终端?
  • 您可能需要使用finVizEngine('Scripts/SnP1000_TICKR.csv, SnP1000_DATA.csv),因为SNP1000 是您的基础项目文件夹
  • 同时命名变量input 是一种不好的做法,因为它是内置函数的名称。
  • 总是将代码、数据和完整的错误消息作为文本(不是截图,不是链接)放在有问题的地方(不在评论中)。

标签: python selenium directory path


【解决方案1】:

首先检查它在哪个文件夹中运行代码

import os

print( os.getcwd() )

cwd 表示Current Working Directory

如果它在不同的文件夹中运行,那么你有脚本,那么它也会在不同的文件夹中搜索 csv。

最简单的方法是使用"/full/path/to/SnP1000_TICKR.csv"

但更有用的方法是使用script 获取文件夹的路径 - 像这样

BASE = os.path.abspath(os.path.dirname(__file__))

并使用它来创建文件csv的完整路径

input_full_path  = os.path.join(BASE, "SnP1000_TICKR.csv") 
output_full_path = os.path.join(BASE, "SnP1000_DATA.csv")

finVizEngine(input_full_path, output_full_path)

顺便说一句:

如果您将csv 保留在子文件夹data 中,那么您将需要

input_full_path = os.path.join(BASE, "data", SnP1000_TICKR.csv")

【讨论】:

    猜你喜欢
    • 2013-02-17
    • 1970-01-01
    • 2020-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-27
    • 2022-01-03
    • 1970-01-01
    相关资源
    最近更新 更多