【问题标题】:FileNotFoundError but a neighbor file can find itFileNotFoundError 但邻居文件可以找到它
【发布时间】:2020-07-14 13:21:05
【问题描述】:

尝试使用 Cisco confparse。我有两个 .py 文件和两个 .txt 文件。一个 python 文件可以解析两个 .txt 文件,而另一个在解析两个 .txt 文件时得到 FileNotFoundError。所有文件都在同一个位置:

/Users/christian/Desktop/Networking/Testing//test.py
/Users/christian/Desktop/Networking/Testing//test2.py
/Users/christian/Desktop/Networking/Testing//exampleSwitch.txt
/Users/christian/Desktop/Networking/Testing//exampleSwitch2.txt

test.py 有效:

from ciscoconfparse import CiscoConfParse

# Parse the config into objects
confprse = CiscoConfParse('exampleSwitch.txt')

# Iterate over all the interface objects
for intf_obj in confprse.find_objects('^interface'):
    print("cisconfparse object: ")
    print(intf_obj)
    print(intf_obj.text)

而 test2.py 没有:

from ciscoconfparse import CiscoConfParse


confprse = CiscoConfParse('exampleSwitch2.txt')

错误是:

[FATAL] CiscoConfParse could not open 'exampleSwitch2.txt'
Traceback (most recent call last):
  File "/Users/christian/Desktop/Networking/Testing/venv/lib/python3.8/site-packages/ciscoconfparse/ciscoconfparse.py", line 233, in __init__
    f = open(config, **self.openargs)
FileNotFoundError: [Errno 2] No such file or directory: 'exampleSwitch2.txt'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/Users/christian/Desktop/Networking/Testing/test2.py", line 4, in <module>
    confprse = CiscoConfParse('exampleSwitch2.txt')
  File "/Users/christian/Desktop/Networking/Testing/venv/lib/python3.8/site-packages/ciscoconfparse/ciscoconfparse.py", line 308, in __init__
    raise RuntimeError
RuntimeError

不确定是什么问题。

当前权限:

total 32
drwxr-xr-x  8 christian  staff   272 Jul 14 09:54 .
drwxr-xr-x  5 christian  staff   170 Jul 14 09:54 ..
drwxr-xr-x  7 christian  staff   238 Jul 14 10:20 .idea
-rw-r--r--  1 christian  staff   332 Jul 13 10:26 exampleSwitch.txt
-rw-r--r--@ 1 christian  staff   875 Jul 14 09:06 exampleSwitch2.txt
-rw-r--r--  1 christian  staff  1402 Jul 14 09:01 test.py
-rw-r--r--  1 christian  staff   167 Jul 14 09:54 test2.py
drwxr-xr-x  6 christian  staff   204 Jul 14 08:46 venv

在 PyCharm "exampleSwitch4.txt" 中创建了一个新文件,权限如下:

(venv) (base) Christians-MBP:Testing christian$ ls -la
total 40
drwxr-xr-x  9 christian  staff   306 Jul 15 12:04 .
drwxr-xr-x  5 christian  staff   170 Jul 15 12:04 ..
drwxr-xr-x  7 christian  staff   238 Jul 15 12:04 .idea
-rw-r--r--@ 1 christian  staff   332 Jul 13 10:26 exampleSwitch.txt
-rw-r--r--  1 christian  staff   875 Jul 14 10:47 exampleSwitch3.txt
-rw-r--r--  1 christian  staff   575 Jul 15 12:04 exampleSwitch4.txt
-rw-r--r--  1 christian  staff  1402 Jul 14 09:01 test.py
-rw-r--r--@ 1 christian  staff   167 Jul 15 12:04 test2.py
drwxr-xr-x  6 christian  staff   204 Jul 14 08:46 venv

【问题讨论】:

  • 向我们展示这段代码的输出:import os; print(os.listdir('/Users/christian/Desktop/Networking/Testing'))
  • @JohnGordon ['.idea', 'exampleSwitch.txt', 'exampleSwitch2.txt', 'test.py', 'test2.py', 'venv']
  • 您确定test2.py 的代码如图所示准确吗?您是直接将其剪切粘贴到问题中,还是手动输入?
  • @JohnGordon 很准确,我复制粘贴了。现在,我最初确实在另一个文件夹中创建了该文件,然后如果有帮助,将其移至该文件夹。
  • 也许@Stefan 是对的——这两个txt 文件的权限是什么?

标签: python networking file-not-found ciscoconfparse


【解决方案1】:

确保执行用户具有正确的权限。

【讨论】:

  • 错误是“找不到文件”,而不是“权限被拒绝”,所以这可能不是原因。
  • @Stefan 我如何检查权限?
  • @thebigstapla 如果您使用普通用户执行脚本,那么只需检查您是否可以打开文件
  • @Stefanxyz 是的,我可以在脚本之外手动打开文件
猜你喜欢
  • 1970-01-01
  • 2020-01-29
  • 1970-01-01
  • 1970-01-01
  • 2020-03-05
  • 2012-10-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多