【问题标题】:What should my directory be in this code?我的目录在这段代码中应该是什么?
【发布时间】:2020-04-11 22:06:32
【问题描述】:

https://github.com/nicolas-gervais/predicting-car-price-from-scraped-data/tree/master/picture-scraper

我正在尝试在 google colab 上运行 main.py 文件,但由于我放置的目录而出现错误。我已将路径作为包含 .py 文件的文件夹。我已将该目录作为包含图像的文件夹,但我收到此错误。

python3: can't open file '/content/gdrive/My': [Errno 2] No such file or directory 

代码:

import os

path = '/content/gdrive/My Drive/picture-scraper' #Where the .py files are located

os.chdir(path)

directory = '/content/gdrive/My Drive/Car Dataset' #Where the images are located

files = ['scrape', 'tag', 'save', 'select']

if __name__ == '__main__':
if not os.path.isdir(directory):
    os.mkdir(directory)

[os.system('python ' + path + f'{file}.py ' + directory) for file in files]

【问题讨论】:

  • 尝试使用path = r"/content/gdrive/My Drive/picture-scraper"directory = r"/content/gdrive/My Drive/Car Dataset"
  • 和以前一样的错误
  • 如果你读到错误(can't open file '/content/gdrive/My'),它只会抱怨部分路径,所以我猜,它无法读取完整路径,因为空间。
  • 试试'"/content/gdrive/My Drive/picture-scraper"'(注意双引号)

标签: python path google-colaboratory


【解决方案1】:

不要将其用作循环:

[os.system('python ' + path + f'{file}.py ' + directory) for file in files]

改用这个:

for file in files:
    command = f'python {path}{file}.py {directory}'
    print('Running command:', command)  # for debugging purposes
    os.system(command)

当你这样做时,我怀疑你会看到这个输出:

Running command: python /content/gdrive/My Drive/picture-scraperscrape.py /content/gdrive/My Drive/Car Dataset
Running command: python /content/gdrive/My Drive/picture-scrapertag.py /content/gdrive/My Drive/Car Dataset
Running command: python /content/gdrive/My Drive/picture-scrapersave.py /content/gdrive/My Drive/Car Dataset
Running command: python /content/gdrive/My Drive/picture-scraperselect.py /content/gdrive/My Drive/Car Dataset

而且我怀疑如果您将这些命令复制并粘贴到命令行中,那么这些命令都不起作用。事实上,您可能会遇到之前看到的错误,即:

python3: can't open file '/content/gdrive/My': [Errno 2] No such file or directory 

要使它们起作用,请更改此行:

command = f'python {path}{file}.py {directory}'

到:

command = f'python "{path}/{file}.py" "{directory}"'

然后你会看到这个输出:

Running command: python "/content/gdrive/My Drive/picture-scraper/scrape.py" "/content/gdrive/My Drive/Car Dataset"
Running command: python "/content/gdrive/My Drive/picture-scraper/tag.py" "/content/gdrive/My Drive/Car Dataset"
Running command: python "/content/gdrive/My Drive/picture-scraper/save.py" "/content/gdrive/My Drive/Car Dataset"
Running command: python "/content/gdrive/My Drive/picture-scraper/select.py" "/content/gdrive/My Drive/Car Dataset"

现在,如果您将这些命令复制并粘贴到终端中,它们应该可以工作。 (事实上​​,这些命令应该已经在运行,所以您甚至不必复制粘贴它们来测试它们。)

(我承认,我没有测试过这个输出。)

如果它确实有效,您可以删除调试行。

我希望这会有所帮助!

【讨论】:

  • 感谢 J-L 它运行 main.py 文件。从 scraper.py 和 tag.py 导出 .csv 文件似乎存在问题,知道问题可能是什么吗?
  • @MTas:如果 scraper.py 和 tag.py 程序在导出 .csv 文件时遇到问题,似乎是它们的问题,而不是主文件(我假设是您在此处发布的文件)。如果不看它们,我无法告诉你它们有什么问题。 (您可以确保它们单独工作,然后它们应该在主文件中工作。)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-06-26
  • 2020-03-02
  • 1970-01-01
  • 1970-01-01
  • 2020-07-30
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多