-1

I need to write a program that reads a cipher text. I'm confused on how to import the needed text. Is this right? What if I have more than one cipher text to import?

def MultiAlphaCipher():
    import MyCipherText.txt
Artjom B.
  • 61,146
  • 24
  • 125
  • 222
jdr93
  • 1
  • Possible duplicate of [Reading entire file in Python](http://stackoverflow.com/questions/7409780/reading-entire-file-in-python) – Artjom B. Dec 10 '15 at 14:41
  • Your question is unclear. What do you want your program to do? What should MultiAlphaCipher() do? What exactly are you trying to import? Please edit your question. – soungalo Dec 10 '15 at 14:43
  • def codeData(fileName): fileName = content content = open('IowaRosterXML.txt', 'r').read() return content – jdr93 Dec 10 '15 at 20:27
  • Never mind! I got this! Thanks for all your help, though! – jdr93 Dec 10 '15 at 20:53

2 Answers2

0

If you are just reading the contents of a plain encrypted text file:

def read_file():
    f=file('MyFile.txt', 'r')  ## For files like ".txt"
    content = f.read()
    f.close()
    return content

file_stuff = read_file()  ## Will return the contents of "MyFile.txt" or whatever the filename is!

There are other conventions on reading a file like using "open" or "with", but that'll do untill you get better... Also, in my experience of cryptography, I suggest using a diffrrent read mode or possibly encoding.

f = file("MyFile.doc", "rb")  ## For files like ".pdf", ".rtf" or any file that uses font, color, etc

That will open the file in "read binary" mode which is helpful if you're encrypting/deceypting files outside plain ASCII. Then when writting to a file, you'll use mode "w" or "wb" to "write binary"

Hope that helps!

Additonal Tips: If that is your actual code:

  • Import is used to import modules/libraries (other programs)
  • Import any module at the beginning and outside of functions/iterations
Chris Nguyen
  • 160
  • 1
  • 4
  • 14
0

Try something like:

with open("MyCipherText.txt") as f:
    cipher_text = f.read()
Gillespie
  • 5,780
  • 3
  • 32
  • 54