Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/sorting/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 目录中没有这样的文件_Python_Selenium Webdriver_Web Scraping_Automation_Selenium Chromedriver - Fatal编程技术网

Python 目录中没有这样的文件

Python 目录中没有这样的文件,python,selenium-webdriver,web-scraping,automation,selenium-chromedriver,Python,Selenium Webdriver,Web Scraping,Automation,Selenium Chromedriver,我是Python新手,我需要将URL列表作为输入传递给脚本,我使用f1=open读取,使用f2=open写入输出 from selenium import webdriver import selenium import time import bs4 import sys f1=open("links.txt","r") urls=f1.readlines() urls=[urls.split()[0] for x in urls] f1.close() f2=open("a.txt"

我是Python新手,我需要将URL列表作为输入传递给脚本,我使用
f1=open
读取,使用
f2=open
写入输出

from selenium import webdriver
import selenium
import time
import bs4
import sys



f1=open("links.txt","r")
urls=f1.readlines()
urls=[urls.split()[0] for x in urls]
f1.close()

f2=open("a.txt","w")
for url in urls:
    driver = webdriver.Chrome()
    driver.get(url)
    time.sleep(5)
    print(driver.current_url)
    f2.write(str(driver.current_url)+"\n")

f2.close()
但我有这个错误

FileNotFoundError:[Errno 2]没有这样的文件或目录:“links.txt”

请帮帮我

FileNotFoundError:[Errno 2]没有这样的文件或目录:“links.txt”

错误提到目录中不存在文件

请注意,您需要检查文件名。我相信您已经通过右键单击新文本文件创建了文本文件,然后将其命名为
links.txt
,因此当您打开文件名时,将其更改为
links.txt
,只需另存为此文件,如快照中所述

并尝试以下选项

f1=打开(“links.txt”、“r”)

或者您可以这样做,而不是像上面提到的那样另存为文件

f1=打开(“links.txt.txt”、“r”)

也可以使用文件的完整路径打开文件

f1=打开(r“D:\Foldername\links.txt.txt”,“r”)

f1=打开(r“D:\Foldername\links.txt”,“r”)

让我知道这个解决方法是否对你有效