简体   繁体   中英

How to wrap the shell command in os.system of Python?

It can get a file named myFile.txt , in which the content is:

file  '/home/debian/1.flv'  
file  '/home/debian/2.flv'  
file  '/home/debian/3.flv'  
file  '/home/debian/4.flv'  
file  '/home/debian/5.flv'  

Now I want to wrap it in Python

os.system("seq  --format="file  '/home/debian/%G.flv'" 5 > myFile.txt" )    
File "<stdin>", line 1  
os.system("seq  --format="file  '/home/debian/%G.flv'" 5 > myFile.txt" )  
                             ^
SyntaxError: invalid syntax

How to Wrap it in Python?

  1. I do understand how to create the file in Python (kvivek)

  2. It is my target to understand how to use complicated shell command in Python,
    If the number of lines is a variable?

There is still a problem with

  1. @Torxed escape character way

     >>> i=7 >>> os.system("seq --format=\\"file '/home/debian/%G.flv'\\" %d > myFile.txt" %i ) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: float argument required, not str 
  2. @ganachoco's triple quotes way

     >>> os.system('''seq --format="file '/home/debian/%G.flv'" %d > myFile.txt" %i''') sh: 1: Syntax error: Unterminated quoted string 

there are two staus to be considered, status 1: the variable is in the shell, we can do (I had verified the following tow shell commands)

root@debian:/home/debian# i=7
root@debian:/home/debian# seq --format="file '/home/debian/%G.flv'" $i > myFile.txt

How to wrap it in Python with os.system?

>>> os.system('i=7')
0
>>> os.system("seq --format=\"file '/home/debian/%G.flv'\" $i > myFile.txt")
seq: missing operand
Try `seq --help' for more information.
256

The variable is in the Python, now I want to call the shell command to put the variable from Python into the shell, how can I do?

>>> i=7  #in python prompt
>>> os.system("seq --format=\"file '/home/debian/%G.flv'\" `here i want to get value from python prompt` > myFile.txt")

quote the " with a backslash

for i in (range(1,5)):
    os.system("seq  --format=\"file  '/home/debian/%d.flv'\" > myFile.txt" %i )

使用三引号

os.system("""seq  --format="file  '/home/debian/%G.flv'" 5 > myFile.txt""")

Hope this gives is what you are looking for

[root@localhost ~]# python
Python 2.6.6 (r266:84292, Apr 11 2011, 15:50:32)
[GCC 4.4.4 20100726 (Red Hat 4.4.4-13)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import os
>>> cmdstr = """i=7 && seq --format="file '/home/kvivek/%G.flv'" $i > myFile.txt"""
>>> os.system(cmdstr)
0
>>> exit()
[root@localhost ~]# cat myFile.txt
file '/home/kvivek/1.flv'
file '/home/kvivek/2.flv'
file '/home/kvivek/3.flv'
file '/home/kvivek/4.flv'
file '/home/kvivek/5.flv'
file '/home/kvivek/6.flv'
file '/home/kvivek/7.flv'
[root@localhost ~]#

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM