3

I am using os.system method in Python to open a file in Linux. But I don't know how to pass the variable (a) inside the os.system command

import os
a=4
os.system('gedit +a test.txt')

How can i pass the variable as an integer inside the command?

1
  • 1
    I believe you meant os.system('gedit +{} test.txt'.format(a)) Commented Dec 9, 2015 at 17:45

2 Answers 2

8
os.system('gedit +%d test.txt' % (a,))

It is recommended to use subprocess instead of os.system:

subprocess.call(['gedit', '+%d' % (a,), 'test.txt'])
Sign up to request clarification or add additional context in comments.

3 Comments

It's good to get in the habit of always using an explicit tuple for the other argument to %: '...' % (a, ).
@chepner Improved. Thanks for nagging. :)
@cherry84 You can accept an answer if it solves your problem.
0
os.system("gedit +" + str(a) + " test.txt")

2 Comments

It gives an error (cannot concatenate 'str' and 'int' objects)
my bad ,All fixed now, You have to cast the integer to a string

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.