私は4つのユーザー入力項目(2つの入力ファイルと2つの出力ファイル)を受け入れるためにgetoptsを使ってスクリプトを書いています。しかし何らかの理由で私はこのエラーが発生し続ける:IOErrorの原因を突き止めることができません
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())
シンプルなファイルは、提供されたパスにありません –
幸い、ファイルは存在しますが、自分で作成しました。 – Sina
ファイルが存在する可能性がありますが、名前を含むはずの変数は実際には設定されていません。私はそれらを未初期化のままにしておくことから始めます。設定されていない変数を使用しようとすると、 'NameError'が返されます。 – chepner