8

I need to execute the following command through python. rtl2gds is a tool which reads in 2 parameters: Path to a file and a module name

rtl2gds -rtl=/home/users/name/file.v -rtl_top=module_name -syn

I am reading in the path to the file and module name from the user through argparse as shown below:

parser = argparse.ArgumentParser(description='Read in a file..')    
parser.add_argument('fileread', type=argparse.FileType('r'), help='Enter the file path')    
parser.add_argument('-e', help='Enter the module name', dest='module_name')    
args = parser.parse_args()    
os.system("rtl2gds -rtl=args.fileread -rtl_top=args.module_name -syn")

But the file path that is read into args.fileread does not get in to the os.system when I call -rtl=args.fileread. Instead, args.fileread itself is being assumed as the file name and the tool flags an error.

I am sure there is a way to read in command line arguments into os.system or some other function (may be subprocess?- but couldnt figure out how). Any help is appreciated.

Nanditha
  • 309
  • 1
  • 6
  • 14
  • possible duplicate of [Calling an external command in Python](http://stackoverflow.com/questions/89228/calling-an-external-command-in-python) – Wolf Sep 24 '14 at 12:23

2 Answers2

20

Don't use os.system(); subprocess is definitely the way to go.

Your problem though is that you expect Python to understand that you want to interpolate args.fileread into a string. As great as Python is, it is not able to read your mind like that!

Use string formatting instead:

os.system("rtl2gds -rtl={args.fileread} -rtl_top={args.module_name} -syn".format(args=args)

If you want to pass a filename to another command, you should not use the FileType type option! You want a filename, not an open file object:

parser.add_argument('fileread', help='Enter the file path')

But do use subprocess.call() instead of os.system():

import subprocess

subprocess.call(['rtl2gds', '-rtl=' + args.fileread, '-rtl_top=' + args.module_name, '-syn'])

If rtl2gds implements command line parsing properly, the = is optional and you can use the following call instead, avoiding string concatenation altogether:

subprocess.call(['rtl2gds', '-rtl', args.fileread, '-rtl_top', args.module_name, '-syn'])
Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
  • It should be `subprocess.call([..., '-syn'` **]** `)`, otherwise it's a `SyntaxError`. – Bakuriu Feb 15 '13 at 10:22
  • 1
    @Bakuriu: Thanks; thick finger typo. – Martijn Pieters Feb 15 '13 at 10:23
  • Thanks for the responses. rtl2gds needs "rtl=". It does not work with just "rtl". When I try the subprocess option: subprocess.call(['rtl2gds', '-rtl=' + args.fileread, '-rtl_top=' + args.module_name, '-syn']) I get the following error: TypeError: cannot concatenate 'str' and 'file' objects – Nanditha Feb 15 '13 at 10:39
  • @Nanditha: did you try that with or without a space in between? (so `rtl2gds -rtl filename`). – Martijn Pieters Feb 15 '13 at 10:40
  • @Nanditha: Ah, yes, you are given an open file, not a filename. Don't use `FileType`.. – Martijn Pieters Feb 15 '13 at 10:42
  • @MartijnPieters, Thanks a lot, this works after deleting the FileType! Btw, I just tried `os.system('rtl2gds -rtl=%s -rtl_top=%s -syn' %(fr, args.module_name))` which was in another thread: [http://stackoverflow.com/questions/9768529/os-system-old-python-and-arguments-with-parameters?rq=1] This works too.. (only after deleting FileType. With FileType, it errors out) – Nanditha Feb 15 '13 at 10:58
  • @Nanditha: Yes, that's the old-style string formatting option. – Martijn Pieters Feb 15 '13 at 11:06
0

Was trying to convert a bunch of .m4a files to .mp3 files, this python script worked for me:

f = os.listdir()

for file in f:

    if(file.endswith('.py')):
        continue

    lastIndex = file.rfind('.')
    name = file[0:lastIndex]
    
    filename = file.replace(' ','\\ ')
    
    name = name.replace(' ','\\ ')
    
    os.system('ffmpeg -i {0} -c:a libmp3lame -q:a 8 {1}.mp3'.format(str(filename), str(name)))

kaustubhd9
  • 109
  • 1
  • 6