我的目录应该在这个代码中是什么



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

我试图在谷歌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]

不要将其用作循环:

[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"

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

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

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

我希望这能有所帮助!

相关内容

  • 没有找到相关文章

最新更新