Python-> Shell:
1. Environment variables
Copy Code code as follows:
Import OS
var=123 or var= ' 123 '
Os.environ[' var ']=str (Var) #environ的键值必须是字符串
Os.system (' echo $var ')
Copy Code code as follows:
Import OS
var=123 or var= ' 123 '
Os.environ[' var ']=str (Var) #environ的键值必须是字符串
Os.system (' echo $var ')
2. String connection
Copy Code code as follows:
Import OS
Path= '/root/a.txt '
VAR=[1]
var= ' Bash '
Os.system (' echo ' + path) #注意echo后有空格
Os.system (' echo ' + str (var[0))
Os.system (' echo ' + var + '/root/c.sh ') #注意echo后和/root with space before
Copy Code code as follows:
Import OS
Path= '/root/a.txt '
VAR=[1]
var= ' Bash '
Os.system (' echo ' + path) #注意echo后有空格
Os.system (' echo ' + str (var[0))
Os.system (' echo ' + var + '/root/c.sh ') #注意echo后和/root with space before
3. Through the pipeline
Copy Code code as follows:
Import OS
Var= ' 123 '
Os.popen (' Wc-c ', ' W '). Write (Var)
Copy Code code as follows:
Import OS
Var= ' 123 '
Os.popen (' Wc-c ', ' W '). Write (Var)
4. Adoption of the document
Copy Code code as follows:
Output = open ('/tmp/mytxt ', ' W ')
Output.write (S) #把字符串S写入文件
Output.writelines (L) #将列表L中所有的行字符串写到文件中
Output.close ()
Copy Code code as follows:
Output = open ('/tmp/mytxt ', ' W ')
Output.write (S) #把字符串S写入文件
Output.writelines (L) #将列表L中所有的行字符串写到文件中
Output.close ()
5. Prepare the output through the redirection marker
Copy Code code as follows:
BUF = open ('/root/a.txt ', ' W ')
Print >> buf, ' 123\n ', ' abc '
Copy Code code as follows:
BUF = open ('/root/a.txt ', ' W ')
Print >> buf, ' 123\n ', ' abc '
Or
Copy Code code as follows:
Print >> open ('/root/a.txt ', ' W '), ' 123\n ', ' abc ' #写入或生成文件
Print >> open ('/root/a.txt ', ' a '), ' 123\n ', ' abc ' #追加
Copy Code code as follows:
Print >> open ('/root/a.txt ', ' W '), ' 123\n ', ' abc ' #写入或生成文件
Print >> open ('/root/a.txt ', ' a '), ' 123\n ', ' abc ' #追加
Shell-> python:
1. Pipeline
Copy Code code as follows:
Import OS
Var=os.popen (' echo-n 123 '). Read ()
Print Var
Copy Code code as follows:
Import OS
Var=os.popen (' echo-n 123 '). Read ()
Print Var
2.
Copy Code code as follows:
Import commands
Var=commands.getoutput (' echo abc ') #输出结果
Var=commands.getstatusoutput (' echo abc ') #退出状态和输出结果
Copy Code code as follows:
Import commands
Var=commands.getoutput (' echo abc ') #输出结果
Var=commands.getstatusoutput (' echo abc ') #退出状态和输出结果
3. Document
Copy Code code as follows:
input = open ('/tmp/mytxt ', ' R ')
S = Input.read () #把整个文件读到一个字符串中
S = Input.readline () #读下一行 (crossed line end flag)
L = Input.readlines () #读取整个文件到一个行字符串的列表中
Copy Code code as follows:
input = open ('/tmp/mytxt ', ' R ')
S = Input.read () #把整个文件读到一个字符串中
S = Input.readline () #读下一行 (crossed line end flag)
L = Input.readlines () #读取整个文件到一个行字符串的列表中