gpt4 book ai didi

python - 无法弄清楚导致 IOError 的原因是什么

转载 作者:行者123 更新时间:2023-12-01 02:54:42 25 4
gpt4 key购买 nike

我已经使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com