Several methods for passing python and shell variables to each other

Source: Internet
Author: User
This article mainly introduces how to pass python and shell variables to each other, using methods such as environment variables and pipelines python-> shell:

1. environment variables

The code is as follows:


Import OS
Var = 123 or var = '20140901'
OS. environ ['var'] = str (var) # The key value of environ must be a string.
OS. system ('echo $ var ')

The code is as follows:


Import OS
Var = 123 or var = '20140901'
OS. environ ['var'] = str (var) # The key value of environ must be a string.
OS. system ('echo $ var ')

2. string connection

The code is as follows:


Import OS
Path = '/root/a.txt'
Var = [1]
Var = 'Bash'
OS. system ('echo '+ path) # note that there are spaces after echo
OS. system ('echo '+ str (var [0])
OS. system ('echo '+ var +'/root/c. sh') # note that there are spaces before echo and/root.

The code is as follows:


Import OS
Path = '/root/a.txt'
Var = [1]
Var = 'Bash'
OS. system ('echo '+ path) # note that there are spaces after echo
OS. system ('echo '+ str (var [0])
OS. system ('echo '+ var +'/root/c. sh') # note that there are spaces before echo and/root.

3. MPs queue

The code is as follows:


Import OS
Var = '000000'
OS. popen ('WC-C', 'w'). write (var)

The code is as follows:


Import OS
Var = '000000'
OS. popen ('WC-C', 'w'). write (var)

4. file

The code is as follows:


Output = open ('/tmp/mytxt', 'w ')
Output. write (S) # write string S to the file
Output. writelines (L) # write all the row strings in list L to the file
Output. close ()

The code is as follows:


Output = open ('/tmp/mytxt', 'w ')
Output. write (S) # write string S to the file
Output. writelines (L) # write all the row strings in list L to the file
Output. close ()

5. redirect standard backup output

The code is as follows:


Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'ABC'

The code is as follows:


Buf = open ('/root/a.txt', 'w ')
Print> buf, '2014 \ n', 'ABC'

Or

The code is as follows:


Print> open ('/root/a.txt', 'w'), '2017 \ n', 'ABC' # write or generate a file
Print> open ('/root/a.txt', 'A'), '2017 \ n', 'ABC' # append

The code is as follows:


Print> open ('/root/a.txt', 'w'), '2017 \ n', 'ABC' # write or generate a file
Print> open ('/root/a.txt', 'A'), '2017 \ n', 'ABC' # append

Shell-> python:

1. MPs queue

The code is as follows:


Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var

The code is as follows:


Import OS
Var = OS. popen ('echo-n 123 '). read ()
Print var

2.

The code is as follows:


Import commands
Var = commands. getoutput ('echo ABC') # output result
Var = commands. getstatusoutput ('echo ABC') # exit status and output result

The code is as follows:


Import commands
Var = commands. getoutput ('echo ABC') # output result
Var = commands. getstatusoutput ('echo ABC') # exit status and output result

3. file

The code is as follows:


Input = open ('/tmp/mytxt', 'r ')
S = input. read () # read the entire file to a string
S = input. readline () # read the next row (the end mark of the row is crossed)
L = input. readlines () # read the entire file to the list of a line string

The code is as follows:


Input = open ('/tmp/mytxt', 'r ')
S = input. read () # read the entire file to a string
S = input. readline () # read the next row (the end mark of the row is crossed)
L = input. readlines () # read the entire file to the list of a line string

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.