1

I would like to replace certain strings which are not hard coded as per my requirement like inputFile,outputFile and outputFileSize eg

COMMAND = """Application.exe inputFile=./Input.txt 
outputFile="Output/Output.txt" outputFileSize=20Kb"""

I would like to do

inputfilename = './File1.txt'

outputfilename = 'Output/File2.txt'

outputfilesize = '90Kb'

So that my replaced string should look like this

COMMAND = """Application.exe inputFile=./File1.txt 
outputFile="Output/File2.txt" outputFileSize=90Kb"""

what is the proper way to do this?

Community
  • 1
  • 1
user1891916
  • 951
  • 1
  • 8
  • 9
  • What about `COMMAND = """Application.exe inputFile={}, outputFile="{}" outputFileSize={}""".format(inputfilename, outputfilename, outputfilesize)`? – j-i-l Aug 01 '15 at 09:11

1 Answers1

0

A possible solution:

command = """Application.exe inputFile=./Input.txt 
outputFile="Output/Output.txt" outputFileSize=20Kb"""

inputfilename = './File1.txt'
outputfilename = 'Output/File2.txt'
outputfilesize = '90Kb'

command = command.replace("./Input.txt", inputfilename)
command = command.replace("Output/Output.txt", outputfilename)
command = command.replace("20Kb", outputfilesize)

print command

Output:

Application.exe inputFile=./File1.txt 
outputFile="Output/File2.txt" outputFileSize=90Kb

Hints:

  • Your input string contains a line break. So it might cause trouble when executing the command.
  • You might want to introduce dedicated placeholders like "INPUT", "OUTPUT" and so on. But using "./Input.txt" works as well.
  • For more information about the string.replace method, have a look into the documentation.
  • As commented by jojo, the approach can be shortened significantly. But I don't know the exact scenario (e.g. why you want to replace the arguments instead of constructing the correct command in the first place).
Falko
  • 17,076
  • 13
  • 60
  • 105
  • thanks i didn't know that it was so easy to do. I was confused with the solution i have searched in google – user1891916 Aug 01 '15 at 09:29
  • @user1891916: you should use string formatting instead (as [@jojo's suggested)](http://stackoverflow.com/questions/31760150/replace-multiple-strings-in-python#comment51453057_31760150) – jfs Aug 03 '15 at 21:31