1

I need to run this python program through my command line, but im not sure how to do so. You're supposed to modify the PATH environment variables before you can do this, correct? I'm using python 3.5 if that helps.

from sys import argv

DEFAULT_KEY = 3

def main() :
   key = DEFAULT_KEY
   inFile = ""
   outFile = ""

   files = 0   # Number of command line arguments that are files.
   for i in range(1, len(argv)) :
      arg = argv[i]
      if arg[0] == "-" :
         # It is a command line option.
         option = arg[1]
         if option == "d" :
            key = -key
         else :
            usage()
            return

      else :
         # It is a file name
         files = files + 1
         if files == 1 :
            inFile = arg
         elif files == 2 :
            outFile = arg

   # There must be two files.
   if files != 2 :
      usage() 
      return 

   # Open the files.
   inputFile = open(inFile, "r")
   outputFile = open(outFile, "w")

   # Read the characters from the file.
   for line in inputFile :
      for char in line :
         newChar = encrypt(char, key)
         outputFile.write(newChar)

   # Close the files.
   inputFile.close()
   outputFile.close()

## Encrypts upper- and lowercase characters by shifting them according to a key.
#  @param ch the letter to be encrypted
#  @param key the encryption key
#  @return the encrypted letter      
#
def encrypt(ch, key) :
   LETTERS = 26   # Number of letters in the Roman alphabet.

   if ch >= "A" and ch <= "Z" :
      base = ord("A")
   elif ch >= "a" and ch <= "z" :
      base = ord("a")
   else :
      return ch    # Not a letter.

   offset = ord(ch) - base + key
   if offset > LETTERS :
      offset = offset - LETTERS
   elif offset < 0 :
      offset = offset + LETTERS 

   return chr(base + offset)

## Prints a message describing proper usage.
#
def usage() :
   print("Usage: python cipher.py [-d] infile outfile")

# Start the program.
main()
Cameron
  • 61
  • 4
  • Was there anything special in that script or would a simple `print('hello world')` have sufficed? Programs need to be (1) copied to a directory in the path, or (2) include path-to-program on the command line or (3) add directory with the program to PATH. – tdelaney Dec 17 '15 at 02:47
  • @tdelaney This is for a class where I have to basically run the code and explain the way that everything within it works. I've done all the explanation of the code, but can't get it to run. The path of the program and the files I'm trying to use in the arguments are the same – Cameron Dec 17 '15 at 02:54
  • i tried ur program and it runs on Ubuntu 14.04 python3.4 but dosen't print out anything because i just used a blank infile, i tried putting some characters in the infile and i'm getting an error. so definitely it runs...so it might be running but the infile is empty....also the out file is not generated so you might want to look at your code also – danidee Dec 17 '15 at 03:13
  • 1
    When working through a problem like this its best to have a short demonstration program that illustrates the problem. @danidee took the time to modify and run your script, but it would have been easier to just have a brain dead "hello world" example program. – tdelaney Dec 17 '15 at 03:19

2 Answers2

1

Have you tried ...

C:>python code.py arguments

Check this link to know about setting path variables if the above fails. How to add to the pythonpath in windows 7?

Community
  • 1
  • 1
astrosyam
  • 857
  • 4
  • 15
  • you need to be in the same path as your code...eg. if your code is in C:\mycode\code.py then the command will be C:\mycode\python code.py arguments – astrosyam Dec 17 '15 at 02:51
  • I just tried that and it didn't throw up any errors this time, but it didn't actually run the program – Cameron Dec 17 '15 at 02:52
  • 1
    since the program does not print anything, you might want to check if the outfile has been generated in the same directory as the code... – astrosyam Dec 17 '15 at 02:57
0

If you saved the program as cipher.py Run the program

python cipher.py infile outfile.json

Check outfile.json is created...

autopython
  • 41
  • 4