0
def file_contents():
    global file_encrypt
    encryption_file = input("What is the name of the file?")
    file_encrypt = open(encryption_file, 'r')
    contents = file_encrypt.read()
    print (contents)
    ask_sure = input("Is this the file you would like to encrypt?")
    if ask_sure == "no":
        the_menu()

This part of the code opens the file the user enters, right? There are no real problems here.

def key_offset():
    key_word = ''
    count = 0
    total = 0
    while count < 8:
        num = random.randint (33, 126)
        letter = chr(num)
        key_word = key_word + letter    
        count = count + 1
        offset = ord(letter)
        total = total + offset
    print("Make sure you copy the key for decryption.")
    if count == 8:
        total = total/8
        total = math.floor(total)
        total = total - 32
        print(key_word)
        return total

This is the part where it calculates the offset and etc etc. Once again no problems here.

def encrypting():
    file = file_contents()
    total = key_offset()
    encrypted = ''
    character_number = 0
    length = len(file_encrypt)

And then this is where the problem appears, I have made the variable file_encrypt global in the first block of code, so therefore it should work. I have tried calling it under another variable like file_en = file_encrypt and used file_en in the length calculating, but it keeps saying it has no length... I have tried asking friends and my teacher, but they seem clueless. The problem is that every time i get to this part it says that file_encrypt has no length or the other way I tried it, file_en has no length, something to do with TextWrapper.io.

Holloway
  • 6,412
  • 1
  • 26
  • 33
  • Python `file` objects don't have a length. If you want to know how big a file is, either read all the bytes out of it and see the length of that, or use something like http://stackoverflow.com/questions/2104080/how-to-check-file-size-in-python – Tom Dalton Jan 28 '16 at 10:53
  • Well, according to [docs](https://docs.python.org/2/library/stdtypes.html#file-objects) file object DOES NOT have length. I don't see anywhere a definition what `len(file_object)` should return. Data read from file may have length (as data will be string, and length for strings is defined), but file object does not. – Łukasz Rogalski Jan 28 '16 at 10:53
  • So if I change the global and everything to the actual name of the file, so in this case, encryption_file, would that work? – Bobby James Jan 28 '16 at 10:55
  • to get size of file on disk use `os.stat(your_filename).st_size` or `os.path.getsize(your_filename)` – furas Jan 28 '16 at 11:05
  • `file = file_contents()` will assign `None` to `file` because `file_contents()` returns `None`. So you need to `return contents` – PM 2Ring Jan 28 '16 at 11:13

2 Answers2

2

file_encrypt is a file pointer, which does indeed not have a length. The contents of your file are in contents, but that is a variable local to the file_contents function.

Really you should not be using global variables; there isn't any reason to here. Instead, return the actual data - contents - from file_contents, then you can use it in the calling function.

Daniel Roseman
  • 588,541
  • 66
  • 880
  • 895
1

There are a few issues with your code, but ignoring those for now, I think your main problems are:

1) The function "file_contents" doesn't return anything, I suspect you want to return "contents". Hard to say without knowing what you want to do with the "file" variable.

def encrypting():
    file = file_contents()  #  <--

2) As others have said, "file_encrypt" is a pointer to a file, although in this function you didn't declare it as global, so it's probably None.

def encrypting():
    file = file_contents()
    total = key_offset()
    encrypted = ''
    character_number = 0
    length = len(file_encrypt)  # <--

So, these modifications should give you what you need:

def file_contents():
    global file_encrypt
    encryption_file = input("What is the name of the file?")
    file_encrypt = open(encryption_file, 'r')
    contents = file_encrypt.read()
    print (contents)
    ask_sure = input("Is this the file you would like to encrypt?")
    if ask_sure == "no":
        the_menu()
    return contents  # <-- ADDED

def encrypting():
    contents = file_contents()  # <-- MODIFIED
    total = key_offset()
    encrypted = ''
    character_number = 0
    length = len(contents)  # <-- MODIFIED
jurasource
  • 406
  • 5
  • 6