0

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")
1
  • os.system("seq --format=\"file '/home/debian/%G.flv'\" 5 > myFile.txt" ) ? Commented Feb 11, 2013 at 10:01

3 Answers 3

3

use triple quote

os.system("""seq  --format="file  '/home/debian/%G.flv'" 5 > myFile.txt""")
Sign up to request clarification or add additional context in comments.

Comments

3

quote the " with a backslash

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

Comments

0

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 ~]#

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.