468,288 Members | 1,963 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,288 developers. It's quick & easy.

How to add an extension to an input filename?

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
5 22819
ghostdog74
511 Expert 256MB
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
6,596 Expert 4TB
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
runsun
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
runsun
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
6,596 Expert 4TB
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.

Similar topics

9 posts views Thread by {AGUT2}=IWIK= | last post: by
12 posts views Thread by Sharad Gupta | last post: by
27 posts views Thread by gmtonyhoyt | last post: by
10 posts views Thread by Brian Gruber | last post: by
32 posts views Thread by ramesh54 | last post: by
reply views Thread by NPC403 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.