繁体   English   中英

Python无法打开文件

[英]Python cannot open file

#!/usr/bin/python 
data = open("/home/mia/Desktop/results/all-nodup.txt", "r")
fd = open("/home/mia/Desktop/results/all-filter.txt", "w")

last_time = 0.0
last_ip = None
last_hash = None
row = data.read()

for line in row:
      timestamp, ip, hash_value = line.split()

      if ip==last_ip and hash_value==last_hash:  

           if float(timestamp) - float(last_time) >= 5.0:
             fd.write("%s\t%s\t%s\n" % (str(timestamp), str(ip), str(hash_value))) 
             last_time, last_ip, last_hash = timestamp, ip, hash_value
      else:
           fd.write("%s\t%s\t%s\n" % (str(timestamp), str(ip), str(hash_value)))   
           last_time, last_ip, last_hash = timestamp, ip, hash_value

fd.close()

这是我的全部代码,我转到results /目录运行: python filter.py我收到一条错误消息: python: can't open file 'filter.py': [Errno 2] No such file or directory

但是其他所有脚本都可以执行,因此python工作正常,也许在这种情况下我应该导入一些内容?

python甚至找不到您的filter.py脚本文件,因此更改代码毫无用处。 要解决此问题,您要么需要:

  • filter.py放在results/目录中
  • 使用绝对路径,例如python /path/to/script/filter.py
  • 找出正确的相对路径,例如python ../../blah/filter.py
  • 将filter.py所在的路径放入PATH变量中

这是因为filter.py不在您运行命令的目录中。

试试python /path/to/filter.py 您也可以使用相对路径,例如,如果文件在一个目录中,则使用python ../filter.py

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM