473,670 Members | 2,307 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to add an extension to an input filename?

17 New Member
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 29856
ghostdog74
511 Recognized Expert Contributor
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 Recognized Expert Expert
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 New Member
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 New Member
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 Recognized Expert Expert
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
3203
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 "stereolithography" file (*.STL) into my program. This has the following syntax... Begin STL Snippet **********
12
15853
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 image name in the same fashion using =onclick showimage(this). Is this possible at at I have tried all combinations of document.getElementbyId(); document.getElementbyName(); document.getElementbyTagName();
27
4634
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 asking here to see what more effective or accepted methods for doing would be presented. The issue is this. My application accepts files names, intended for a cross platform unix and windows environment, from the command line. So the application...
0
1695
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 installed those critical updates. Now I can not chaned or add any application extension mapping. When I try to change the .xyz extension in the Add/Edit Application Extension Mapping window, the extension input field is disabled. And when I try to add...
10
13060
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 generated number from a program that I saved from. So I need to find a way that I can quickly remove the underscore and the last 4 numbers from the filename without changing anything else. I can't set a max length to the file becuase the filename's...
0
1944
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 http://www.google.com/url?sa=D&q=http://msdn.microsoft.com/library/default.asp%3Furl%3D/library/en-us/fileio/fs/naming_a_file.asp does not work: >>> os.path.getmtime('\\\\?\\'+fabs) Traceback (most recent call last): File "<interactive input>", line 1, in ?
32
2410
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 d124_RD_SF_t01_r05_s089_z121_18grad.txt folders and sub folders should be as follows 1. /x01_a05_s089/RD_z121_18grad
8
2654
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
988
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 on how to catch this as an error? Oh - the reason why the input file is ".." is because the Windows filepath uses the backslashes so I think they are seen as escape characters, so ..\..\bin\filename.txt only becomes ".." through my regex parser. ...
0
8386
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8814
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8661
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7419
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6213
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4211
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4391
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2800
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2042
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.