没有这样的文件或目录,但是文件存在?

时间:2020-04-08 09:39:35

标签: python file directory

我正在编写一个python脚本,我需要在其中打开“ .txt”文件夹并分析其中的文本。

我已将此“ .txt”文档保存在与Python脚本相同的文件夹中。

但是,当我去打开文件时; file = open("words.txt",'r')

我收到错误消息:No such file or directory: 'words.txt'

我不明白为什么会这样?

4 个答案:

答案 0 :(得分:2)

Maby,这是因为您当前的工作目录与文件存储的目录不同。尝试提供文件的完整路径

file = open("<full_path>\words.txt",'r')

答案 1 :(得分:0)

  • 检查代码或文件名中是否有错字
  • 确保文件位于当前工作目录下确实。有时,IDE中显示的类似文件名或信息会引起混乱
  • 确保您正在编辑正确的脚本。有时人们将脚本复制并粘贴到不同的位置,然后立即忘记他们实际上正在编辑的脚本

希望有帮助。

答案 2 :(得分:0)

这是因为您正在使用的.py文件的目录与.txt文件的路径不同。

因此,您需要提及这样的路径:

file = open("C:/User/Desktop/Folder/words.txt", 'r')

答案 3 :(得分:-1)

尝试使用以下一项:public void add inputs(){ // here I will get two elements int the list List<Webelement> labels = BrowserFactory.getdriver().findElements(By.xPath("//div/label[@class='ng.binding']") // here I will get two elements related to select in the list List<Webelement> selectDropdown= BrowserFactory.getdriver().findElements(By.xPath("//div/select[@class='ng-touched']") for (Webelement label: labels ) { if (label.getText().equals("Target Environment")) { // here I have to click the select(dropdown) of the matched label i.e Target Environement' select class } } } 您在Windows上吗?