473,395 Members | 2,437 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

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 29691
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

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

Similar topics

9
by: {AGUT2}=IWIK= | last post by:
Hello all, It's my fisrt post here and I am feeling a little stupid here, so go easy.. :) (Oh, and I've spent _hours_ searching...) I am desperately trying to read in an ASCII...
12
by: Sharad Gupta | last post by:
i have this problem of capturing the filename on the instance when onclick is activated in the <body> the function should catch the filename and display it. Second problem, i have to catch the...
27
by: gmtonyhoyt | last post by:
I need assistance coming up with a clean way to handle filename extensions with my application. While I can come up with several ways of doing so on my own, I felt perhaps it would be worth...
0
by: Leo | last post by:
Hi, I'm running Windows XP professinal and have an application extension mapping in IIS that maps .xyz to aspnet_isapi. Everything works fine until I did some Windows Components Updates and...
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
0
by: robert | last post by:
e.g. open/os module functions (os.path.getmtime...) and win32api/win32file functions fail on long paths (>~255 chars) even the '\\?\' trick from...
32
by: ramesh54 | last post by:
Hello All, I have a small problem in reading the filename of a file. I would like to have a script which could read my file and create the respective folders, The file to be read is as follows...
8
by: gobblegob | last post by:
Hi guys, i am trying to add .txt extension to the filename. $filename = (isset($_POST)) ? $_POST : '' ; Thanks in advanced.
0
by: dissectcode2 | last post by:
I check the return value of fseek when the input filename is ".." and on Linux it returns -1, but on Cygwin it returns 0! I know that ".." is technically a directory. Can anyone give me some advice...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.