Python Script
核心语法
注释
#!/usr/bin/python3
# 第一个注释
'''
注释
'''
print("Hello,Python!")
输入输出
python实现
str = input("please input:")
print()
bash实现
echo "please input"
read str
if逻辑
if command:
x
elif command2:
y
else:
文件读写
获取所有内容
file = fopen("filename")
lines = file.read()
# lines = file.readline() 只读一行
print(lines)
函数
def function(parameters):
"函数文档
function_work
return
导入包
常用模块
获取命令行参数(sys)
import sys
print('Number of arguments:',len(sys.argv))
print('Argument list :',str(sys.argv))
输出结果:
python3 script-io.py 1 a
Number of arguments: 3
Argument list : ['script-io.py', '1', 'a']
open
with open('data.txt') as f:
print(f.read())
with open('data.txt',mode='w') as f:
f.write('this is the new content will write to the file')
os
>>> import os
>>> entries = os.listdir() # 当前目录
>>> entries
['script-io.py', 'filename']