2017-06-02 50 views
1

我已經使用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()) 
+1

簡單,文件不存在提供的路徑 –

+0

幸運的是,文件確實存在,我自己創建它們。 – Sina

+1

這些文件可能存在,但應該包含其名稱的變量實際上並未設置。我會開始讓它們不被初始化;當你嘗試使用未設置的變量時,你會得到一個'NameError'。 – chepner

回答

2

下面是使用​​代替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() 
+0

這就像是超級快。到達之前,我幾乎沒有離開座位。謝謝! – Sina

相關問題