By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
439,957 Members | 2,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 439,957 IT Pros & Developers. It's quick & easy.

How to add an extension to an input filename?

P: 17
Read from an existing file, then output a file with the same name but an extension:

file = raw_input ('Enter the filename:\n')
fileout = open ('file.out', 'w')

However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
May 19 '07 #1
Share this Question
Share on Google+
5 Replies


Expert 100+
P: 511
Read from an existing file, then output a file with the same name but an extension:

file = raw_input ('Enter the filename:\n')
fileout = open ('file.out', 'w')

However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
Don't use file as variable name.
you wanted a .out extension for the output filename that the user types. therefore you should declare your filename as:
Expand|Select|Wrap|Line Numbers
  1. filename = raw_inpu("blah....")
  2. filename = filename + ".out")
  3. fileout = open(filename,"w")
  4. ....
  5.  
May 19 '07 #2

bartonc
Expert 5K+
P: 6,596
Read from an existing file, then output a file with the same name but an extension:

file = raw_input ('Enter the filename:\n')
fileout = open ('file.out', 'w')

However, when I enter a filename like "test", it gives the output file as "file.out" instead of "test.out".
Ok. Here's what you want to do: Open the file that has your input in "r"ead mode. Read the data. Close the input file. Do some work on the data. Open a new file for output in "w"rite mode. write the data. Close the output file. Basically:
Expand|Select|Wrap|Line Numbers
  1. inputFileName = raw_input ('Enter the filename:\n')
  2. outputFileName = inputFileName + ".out"
  3.  
  4. inFile = open(inputFileName, 'r')
  5. dataAsList = inFile.readlines()
  6. inFile.close()
  7.  
  8. # simulate some work here
  9. for line in dataAsList:
  10.     print line
  11.  
  12. outFile = open (outputFileName, 'w')
  13. outFile.writelines(dataAsList)
  14. outFile.close()
  15.  
Hope that helps.
May 19 '07 #3

P: 17
That works well. Thank ghostdog74!

Don't use file as variable name.
you wanted a .out extension for the output filename that the user types. therefore you should declare your filename as:
Expand|Select|Wrap|Line Numbers
  1. filename = raw_inpu("blah....")
  2. filename = filename + ".out")
  3. fileout = open(filename,"w")
  4. ....
  5.  
May 19 '07 #4

P: 17
Thank you very much!
Not only did you answer this extension question, but also answered those related to my previous questions!

Ok. Here's what you want to do: Open the file that has your input in "r"ead mode. Read the data. Close the input file. Do some work on the data. Open a new file for output in "w"rite mode. write the data. Close the output file. Basically:
Expand|Select|Wrap|Line Numbers
  1. inputFileName = raw_input ('Enter the filename:\n')
  2. outputFileName = inputFileName + ".out"
  3.  
  4. inFile = open(inputFileName, 'r')
  5. dataAsList = inFile.readlines()
  6. inFile.close()
  7.  
  8. # simulate some work here
  9. for line in dataAsList:
  10.     print line
  11.  
  12. outFile = open (outputFileName, 'w')
  13. outFile.writelines(dataAsList)
  14. outFile.close()
  15.  
Hope that helps.
May 19 '07 #5

bartonc
Expert 5K+
P: 6,596
Thank you very much!
Not only did you answer this extension question, but also answered those related to my previous questions!
You are welcome, very much.
Thank you for joining TheScripts.com.
Keep posting,
Barton
May 19 '07 #6

Post your reply

Sign in to post your reply or Sign up for a free account.