Python file read and write operations are performed interactively with the Linux shell variable commands

Source: Internet
Author: User
This example describes how Python file read and write operations interact with the Linux Shell variable command. Share to everyone for your reference. Specific as follows:

Python is easy to read and write to files, and the interaction variables with the Linux shell need to be converted to be used, which is more of a headache.

The code is as follows:

The code is as follows:


#coding =utf-8
#!/usr/bin/python
Import OS
Import time
#python执行linux命令
Os.system (': >./aa.py ')
#人机交互输入
S = Raw_input ("Input:")
os.environ[' s ']=str (s)
#把字符串S写入文件
Output = open ('./aa.py ', ' a ')
Output.write (S)
#关闭文件
Output.close ()
#python中的思考时间
Time.sleep (1)
#获取文件内容
f = open ('./aa.py ', ' R ')
Read = F.read ()
#变量间的转换
os.environ[' read ']=str (read)
Os.system (' $read ')
F.close ()
Var=os.popen (' Ifconfig '). Read ()
Print Var

Hopefully this article will help you with Python programming.

  • Related Article

    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.