Welcome to Code Forum!

Join a community that supports you and your coding journey from day one. We strive to be a friendly, supportive community that empowers everyone to be better developers. By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.

    GIF shows where to locate </> in the thread and or post editor toolbar.
    To learn more about how to use our BBCode feature, review our "How to post your code into threads" here.

    Thank you, Code Forum.

Python Trying to do some basic error checking

Insomniac

New Coder
Trying to do some basic error checking. Only started with Python a few days ago. I'm confused why the following code does not work. No matter whether I start the program with 3 or 4 arguments, I always get the error message, and I don't understand why that happens. Similar code has worked for me before in other programs. Please enlighten me what is happening here.

Trying to start the program as ./project_decoder.py blah out1 out2 out3 on a Linux machine. out1, out2, out3 are files which exist with 2329 filesize. blah is not an existing file.

Python:
# some basic error checking
no_third_file = False
try:
    outputfile = sys.argv[1]
    inputfile1 = sys.argv[2]
    inputfile2 = sys.argv[3]
    inputfile3 = sys.argv[4]
except:
    outputfile = sys.argv[1]
    inputfile1 = sys.argv[2]
    inputfile2 = sys.argv[3]
    no_third_file = True
finally:
    print('Not enough command line arguments found. Usage:')
    print(sys.argv[0], 'outputfile inputfile1 inputfile2 inputfile3')
    print('Total output size is expected to be about two thirds of total input size')
    print('If only two input files are available, the third can be omitted')
    print('In that case, xor will be used to reconstruct the third file')
    quit()
 
Here is the rest of the file in case the context matters. I'd like to write the rest of it, but if it won't take input, that's a showstopper.

Python:
#!/usr/bin/python3

import os.path
import sys
import hashlib
from bitstring import BitArray, BitStream

debug_level = 1 # 0 = minimal output, 1 = most stuff, 2 = everything
write_files = True
compute_hashes = False

# compute md5 hash with 8kb buffer
def get_md5_hash(filename, buffer_size=2**10*8):
    file_hash = hashlib.md5()
    with open(filename, mode="rb") as f:
        chunk = f.read(buffer_size)
        while chunk:
            file_hash.update(chunk)
            chunk = f.read(buffer_size)
    return file_hash.hexdigest()

# compute sha256 hash with 8kb buffer
def get_sha256_hash(filename, buffer_size=2**10*8):
    file_hash = hashlib.sha256()
    with open(filename, mode="rb") as f:
        chunk = f.read(buffer_size)
        while chunk:
            file_hash.update(chunk)
            chunk = f.read(buffer_size)
    return file_hash.hexdigest()

# compute sha512 hash with 8kb buffer
def get_sha512_hash(filename, buffer_size=2**10*8):
    file_hash = hashlib.sha512()
    with open(filename, mode="rb") as f:
        chunk = f.read(buffer_size)
        while chunk:
            file_hash.update(chunk)
            chunk = f.read(buffer_size)
    return file_hash.hexdigest()

# some basic error checking
no_third_file = False
try:
    outputfile = sys.argv[1]
    inputfile1 = sys.argv[2]
    inputfile2 = sys.argv[3]
    inputfile3 = sys.argv[4]
except:
    outputfile = sys.argv[1]
    inputfile1 = sys.argv[2]
    inputfile2 = sys.argv[3]
    no_third_file = True
finally:
    print('Not enough command line arguments found. Usage:')
    print(sys.argv[0], 'outputfile inputfile1 inputfile2 inputfile3')
    print('Total output size is expected to be about two thirds of total input size')
    print('If only two input files are available, the third can be omitted')
    print('In that case, xor will be used to reconstruct the third file')
    quit()

if not os.path.isfile(inputfile1):
    print('error: input file 1 not found')
    quit()

if not os.path.isfile(inputfile2):
    print('error: input file 2 not found')
    quit()

if not no_third_file and not os.path.isfile(inputfile3):
    print('error: input file 3 not found')
    quit()

try:
    input_filesize1 = os.path.getsize(inputfile1)
    input_filesize2 = os.path.getsize(inputfile2)
    input_filesize3 = os.path.getsize(inputfile3)
except:
    print('error: cannot read file sizes for input files')
    quit()

if not input_filesize1 == input_filesize2 and input_filesize2 == input_filesize3:
    print('error: input files do not have the same size')
    quit()

if input_filesize1 == 0 or input_filesize2 == 0 or input_filesize3 == 0:
    print('error: input file sizes are expected to be greater than zero')
    quit()

if input_filesize1 > 2 ** 30 * 2: #2GB
    print('error: program is not intended to work with large (2GB+) files currently')
    quit()

if os.path.isfile(outputfile):
    print('error: outputfile already exist')
    quit()

# gather information about input files
 

Buy us a coffee!

Buy me a coffee.
Back
Top Bottom