gpt4 book ai didi

python - 通过命令行运行这个python程序

转载 作者:太空宇宙 更新时间:2023-11-03 17:08:54 24 4
gpt4 key购买 nike

我需要通过命令行运行这个 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()

最佳答案

你有没有尝试过...

C:>python code.py 参数

如果上述方法失败,请检查此链接以了解如何设置路径变量。 How to add to the pythonpath in windows 7?

关于python - 通过命令行运行这个python程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34325712/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com