- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我已经使用 getopts 编写了一个脚本来接受四个用户输入项(两个输入文件和两个输出文件)。但由于某种原因,我不断收到此错误:
python2.7 compare_files.py -b /tmp/bigfile.txt -s /tmp/smallfile.txt -n /tmp/notinfile.txt -a /tmp/areinfile.txt
compare_files.py -b <biggerfile> -s <smallerfile> -n <notinfile> -a <areinfile> d
I/O error: [Errno 2] No such file or directory: ''
(<type 'exceptions.IOError'>, IOError(2, 'No such file or directory'), <traceback object at 0x7f10c485c050>)
我无法理解哪个输入项变量给我带来了问题。请有人告诉我哪里出了问题?这是脚本:
import sys, getopt
def main(argv):
binputfilename = ''
sinputfilename = ''
outputfilename1 = ''
outputfilename2 = ''
try:
opts, args = getopt.getopt(argv, "h:b:s:n:a", ["bfile=", "sfile=", "nfile=", "afile="])
except getopt.GetoptError as (errno, strerror):
print(str(sys.argv[0]) + " -b <biggerfile> -s <smallerfile> -n <notinfile> -a <areinfile> a")
print("GetOpts error({0}): {1}".format(errno, strerror))
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print(str(sys.argv[0]) + " -b <biggerfile> -s <smallerfile> -n <notinfile> -a <areinfile> b")
sys.exit()
elif opt in ("-b", "--bfile"):
binputfilename = arg
elif opt in ("-s", "--sfile"):
sinputfilename = arg
elif opt in ("-n", "--nfile"):
outputfilename1 = arg
elif opt in ("-a", "--afile"):
outputfilename2 = arg
elif len(sys.argv[1:]) > 4 or len(sys.argv[1:]) < 4:
print(str(sys.argv[0]) + " -b <biggerfile> -s <smallerfile> -n <notinfile> -a <areinfile> c")
sys.exit(2)
#smallset = set(open(sinputfilename).read().split())
#bigset = set(open(binputfilename).read().split())
with open(sinputfilename, 'r') as smallsetsrc, open(binputfilename, 'r') as bigsetsrc, open(outputfilename1, 'w') as outfile1, open(outputfilename2, 'w') as outfile2:
smallset = set(line.strip() for line in smallsetsrc)
bigset = set(line.strip() for line in bigsetsrc)
#find the elements in smallfile that arent in bigfile
outset1 = smallset.difference(bigset)
#find the elements in small file that ARE in bigfile
outset2 = smallset.intersection(bigset)
count = 0
for msisdn in outset1:
count += 1
#outfile1.write("%s, %s, %s, %s\n" % (str(count)+".", msisdn))
print(str(count), msisdn)
# count = 0
# for msisdn in outset2:
# count += 1
# outfile2.write("%s, %s, %s, %s\n" % (str(count)+".", msisdn))
if __name__ == "__main__":
try:
main(sys.argv[1:])
except IOError, e:
print(str(sys.argv[0]) + " -b <biggerfile> -s <smallerfile> -n <notinfile> -a <areinfile> d")
print("I/O error: {0}".format(str(e)))
print(sys.exc_info())
最佳答案
这是使用 argparse
而不是 getopts
的代码,这应该可以更好地确保指定正确的名称。 (通常,我不喜欢必需的选项;我会使用位置参数。)
请注意,您不需要同时打开这些文件;只打开您需要的那个,并且只在您使用时打开。
from __future__ import print_function
import argparse
def main():
p = argparse.ArgumentParser()
p.add_argument("-b", "--bfile", required=True)
p.add_argument("-s", "--sfile", required=True)
p.add_argument("-n", "--nfile", required=True)
p.add_argument("-a", "--afile" required=True)
args = p.parse_args()
with open(args.sfile) as smallsetsrc:
smallset = set(line.strip() for line in smallsetsrc)
with open(args.bfile) as bigsetsrc:
bigset = set(line.strip() for line in bigsetsrc)
outset1 = smallset.difference(bigset)
outset2 = smallset.intersection(bigset)
with open(args.nfile, "w") as out:
for count, msisdn in enumerate(outset1):
print("%d. %s" % (count, msisdn), file=out)
with open(args.afile, "w") as out:
for count, msisdn in enumerate(outset2):
print("%d. %s" % (count, msisdn), file=out)
if __name__ == "__main__":
main()
关于python - 无法弄清楚导致 IOError 的原因是什么,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44335661/
我有一个 python 脚本,它创建一个 tar 文件,将文件移动到 tar 文件中,然后删除它们。 我可以毫无问题地手动运行脚本。但是当它从 cron 运行时,它失败了: IOError: [Err
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
我正在尝试从我的Python脚本打开文件recentlyUpdated.yaml。但当我尝试使用时:。我收到一个错误,内容是:。为什么?我怎样才能解决这个问题?
尝试将数据加载为 Excel 作为响应时,出现 IO 请求数据读取错误。 def convert_to_excel(request): field = forms.CharField()
我正在尝试读取文件夹树中的一系列 DICOM 文件,并且我使用下面的代码来运行树,边读取每个文件。问题是我收到确实存在的文件的 IOErrors,我已经检查了文件权限和其他 SO 线程,例如 Pyth
我有一个类可以读取特定格式的文件。这些文件的大小往往大于 8Gb,因此通常会进行压缩。在读取文件时,我想捕获文件未被压缩的错误,但 except IOError: 和 except: 都不会这样做,出
这个问题在这里已经有了答案: open() gives FileNotFoundError / IOError: '[Errno 2] No such file or directory' (8 个
给定这段代码: try: #do something except IOError as message: logging.error(message)
for subdir, dirs, files in os.walk(crawlFolder): for file in files: print os.getcwd()
我正在尝试导入模块并创建其类的对象,如下所示: >>> import scriptsim >>> scriptsim.Simulator() 但出现以下错误: Traceback (most rece
我正在使用以下第三方库在 Django 中编写网络应用程序: Django==1.6.1 argparse==1.2.1 cffi==0.8.1 pycparser==2.10 pylast==0.5
我正在使用 python-crontab module确保每天下午 2 点运行我的脚本。 但是我在执行脚本时遇到了一些 IOErrors Traceback: File "backup.py", li
我正在编写一个程序来更改我的桌面背景。它通过读取文本文件来完成此操作。如果文本文件显示其中一个 BG 文件名,它会将那个保存为我的背景,并将另一个的名称写入文件并关闭它。 我似乎无法让它工作。 这是我
我从未见过 IOError被抛出。文档中关于 IOError 的唯一内容是: Thrown when a serious I/O error has occurred. 没有任何子类或其他明显的东西。
我正在尝试下载链接并将其放置在downloads文件夹中,但是出现权限错误。我是计算机上的管理员用户,我也以管理员模式运行它。仍然我得到同样的错误。 这是我使用的代码: urllib.urlretri
我正在构建一个 Haskell 应用程序,并试图弄清楚如何构建错误处理机制。在实际的应用程序中,我正在使用 Mongo 进行大量工作。但是,为此,我将通过对文件进行基本 IO 操作来进行简化。 因此,
当我尝试重试失败的任务时,我会间歇性地(大约 20% 的时间)从 Celery 收到 IOError 异常。 这是我的任务: @task def update_data(pk_id): tr
我编写了一个 python 脚本,想要将日志写入/var/log/myapp.log 中的文件。然而,在某些平台上这并不存在,或者我们可能没有这样做的权限。既然如此,我想尝试在其他地方写。 def g
我已经使用 getopts 编写了一个脚本来接受四个用户输入项(两个输入文件和两个输出文件)。但由于某种原因,我不断收到此错误: python2.7 compare_files.py -b /tmp/
我是一名优秀的程序员,十分优秀!