【问题标题】:Python: No such file or directory [duplicate]Python:没有这样的文件或目录[重复]
【发布时间】:2015-04-05 19:16:13
【问题描述】:

我收到一条关于我的 python 代码没有这样的文件或目录的错误消息。我将文件保存在我的台式计算机上。但我收到错误消息

 #!/usr/bin/python
fh=open("C:/Users/****/Desktop/IP Data","r")
for line in fh.readlines():
        line = line.strip()
        words = line.split()
# increase counters
for word in words: 
            if len(words)>3:
                if "IP" in words[1]:
                    print '%s\t%s' % (words[2],1)

【问题讨论】:

  • 尝试将其作为r"C:/Users/****/Desktop/IP Data"中的原始字符串
  • @BhargavRao,你是这样说的吗:fh=open("r C:/Users/****/Desktop/IP Data","r")
  • @BhargavRao,我收到了同样的错误。我的程序在一个装有 Windows 7 的 Virtual Box 中。我安装了 python 2.7
  • 试试这个r'C:\\Users\***\Desktop\IP Data'注意C:后面的双反斜杠
  • @BhargavRao,谢谢伙计

标签: python


【解决方案1】:

我认为您需要在 Windows 中使用反斜杠。试试改成C:\\Users\\***\\Desktop\\IP Data

【讨论】:

  • Vama,我收到了同样的错误。我的程序在一个装有 Windows 7 的 Virtual Box 中。我安装了 python 2.7
  • 如果你在 cmd.exe 中工作,那么可以,但如果你在 Python 中工作,那么你可以使用任何一个。
  • 反斜杠很好,但它们应该被转义以在字符串中结束,不是吗? "C:\\Users\\..."
  • @flaschbier 是的,他们应该被转义。或者可以将字符串声明为原始字符串。
  • @flaschbier :D 完成了!
猜你喜欢
  • 2018-04-13
  • 1970-01-01
  • 1970-01-01
  • 2016-08-11
  • 2014-06-18
  • 2019-12-23
  • 2021-12-10
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多