我需要通过命令行运行这个python程序,但我不知道该怎么做。您应该在执行此操作之前修改PATH环境变量,对吗?如果有帮助,我正在使用python 3.5。
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()
答案 0 :(得分:1)
你试过......
C:&gt; python code.py 参数
如果上述操作失败,请检查此链接以了解有关设置路径变量的信息。 How to add to the pythonpath in windows 7?
答案 1 :(得分:0)
如果您将程序保存为cipher.py 运行程序
python cipher.py infile outfile.json
检查outfile.json是否已创建......