准备在UNIX环境中运行Python程序
这学期刚开始使用UNIX将程序移动到存储库,我很好奇如何准备我的文件以便在该环境中运行 这是我的密码:准备在UNIX环境中运行Python程序,python,unix,Python,Unix,这学期刚开始使用UNIX将程序移动到存储库,我很好奇如何准备我的文件以便在该环境中运行 这是我的密码: def main(file): cTest = 0 words = 0 lines = 0 chars = 0 for l in open(str(file)): linewords = l.split() for w in l: chars += 1 lines += 1
def main(file):
cTest = 0
words = 0
lines = 0
chars = 0
for l in open(str(file)):
linewords = l.split()
for w in l:
chars += 1
lines += 1
words += len(linewords)
print("Lines: " + str(lines))
print("Characters: " + str(chars))
print("Words: " + str(words))
从功能的角度来看,当在我自己的系统上正确指向时,它工作得很好,但现在是在UNIX上,我被告知要像这样运行它
python3 wc.py < file.txt
python3 wc.py
如何准备文件,以便在这种环境中执行时正确地获取文本?幸运的是,这不依赖于平台。由于您可以阅读它或其他地方,您只需导入sys并从stdin读取:
data = sys.stdin.readlines()
但工作开始了:
- 你应该有一个可执行的入口
- 您应该使脚本可执行(
)chmod+x yourfile.py
- 你应该让你的脚本以一些bash魔法开始(第一行)
- 您不应该使用关键字作为变量名(如
)文件
#!/bin/env python
import sys
def main():
data = sys.stdin.readlines()
word_count = 0
char_count = 0
line_count = len(data)
for l in data:
linewords = l.split()
word_count += len(linewords)
for w in l:
char_count += len(w)
print("Lines: %d" % line_count)
print("Characters: %d" % char_count)
print("Words: %d" % word_count)
if __name__ == '__main__':
main()
但正如前面所说的:除了shell魔法和可执行位之外,由于Python,这不是unix特有的。幸运的是,这不依赖于平台。由于您可以阅读它或其他地方,您只需导入sys并从stdin读取:
data = sys.stdin.readlines()
但工作开始了:
- 你应该有一个可执行的入口
- 您应该使脚本可执行(
)chmod+x yourfile.py
- 你应该让你的脚本以一些bash魔法开始(第一行)
- 您不应该使用关键字作为变量名(如
)文件
#!/bin/env python
import sys
def main():
data = sys.stdin.readlines()
word_count = 0
char_count = 0
line_count = len(data)
for l in data:
linewords = l.split()
word_count += len(linewords)
for w in l:
char_count += len(w)
print("Lines: %d" % line_count)
print("Characters: %d" % char_count)
print("Words: %d" % word_count)
if __name__ == '__main__':
main()
但正如前面所说的:除了shell魔法和可执行位之外,由于Python,这不是unix特有的。并遵循上面的建议
import sys
main(str(sys.argv[0]))
在文件末尾添加以下内容。并遵循上面的建议
import sys
main(str(sys.argv[0]))
啊,欢迎来到现实世界: 在Unix/Linux环境中,每一个基于脚本的程序都以“沙邦”开头。“#!”然后是程序的完整路径。之后,程序只需要由env执行即可。这可以通过chmod命令完成。另一个小的调整将使代码更加pyhonic,那就是使用它的_umain__;构造 wc.py
#!/usr/bin/python
# where /usr/bin/python is the full path to your python.
# you can get determine this from doing $ which python
import optparse
def word_count(file):
""" A good programmer always documents there functions"""
cTest = 0
words = 0
lines = 0
chars = 0
for l in open(str(file)):
linewords = l.split()
for w in l:
chars += 1
lines += 1
words += len(linewords)
print("Lines: " + str(lines))
print("Characters: " + str(chars))
print("Words: " + str(words))
if __name__ == '__main__':
p = optparse.OptionParser(description="My Test Program", usage='%prog <filename>')
ons, arguments = p.parse_args()
if len(arguments) == 1:
word_count(arguments[0])
else:
p.print_help()
啊,欢迎来到现实世界: 在Unix/Linux环境中,每一个基于脚本的程序都以“沙邦”开头。“#!”然后是程序的完整路径。之后,程序只需要由env执行即可。这可以通过chmod命令完成。另一个小的调整将使代码更加pyhonic,那就是使用它的_umain__;构造 wc.py
#!/usr/bin/python
# where /usr/bin/python is the full path to your python.
# you can get determine this from doing $ which python
import optparse
def word_count(file):
""" A good programmer always documents there functions"""
cTest = 0
words = 0
lines = 0
chars = 0
for l in open(str(file)):
linewords = l.split()
for w in l:
chars += 1
lines += 1
words += len(linewords)
print("Lines: " + str(lines))
print("Characters: " + str(chars))
print("Words: " + str(words))
if __name__ == '__main__':
p = optparse.OptionParser(description="My Test Program", usage='%prog <filename>')
ons, arguments = p.parse_args()
if len(arguments) == 1:
word_count(arguments[0])
else:
p.print_help()
查看Python中的
sys
库。是谁告诉你这样运行它的,一个讲师、另一个学生还是一个老的粗制滥造的Unix实验室居民?查看Python中的sys
库。是谁告诉你这样运行它的,一个讲师、另一个学生还是一个老的粗制滥造的Unix实验室居民?对于Python 2.7+,optpass被贬损为argparse。除此之外,这是非常好的。对于Python2.7+,optparse被贬损为argparse。除此之外,这是极好的。