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".
5 29856
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: -
filename = raw_inpu("blah....")
-
filename = filename + ".out")
-
fileout = open(filename,"w")
-
....
-
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: -
inputFileName = raw_input ('Enter the filename:\n')
-
outputFileName = inputFileName + ".out"
-
-
inFile = open(inputFileName, 'r')
-
dataAsList = inFile.readlines()
-
inFile.close()
-
-
# simulate some work here
-
for line in dataAsList:
-
print line
-
-
outFile = open (outputFileName, 'w')
-
outFile.writelines(dataAsList)
-
outFile.close()
-
Hope that helps.
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: -
filename = raw_inpu("blah....")
-
filename = filename + ".out")
-
fileout = open(filename,"w")
-
....
-
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: -
inputFileName = raw_input ('Enter the filename:\n')
-
outputFileName = inputFileName + ".out"
-
-
inFile = open(inputFileName, 'r')
-
dataAsList = inFile.readlines()
-
inFile.close()
-
-
# simulate some work here
-
for line in dataAsList:
-
print line
-
-
outFile = open (outputFileName, 'w')
-
outFile.writelines(dataAsList)
-
outFile.close()
-
Hope that helps.
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
Sign in to post your reply or Sign up for a free account.
Similar topics |
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
**********
|
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();
|
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...
|
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...
|
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...
| |
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 ?
|
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
|
by: gobblegob |
last post by:
Hi guys,
i am trying to add .txt extension to the filename.
$filename = (isset($_POST)) ? $_POST : '' ;
Thanks in advanced.
|
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.
...
|
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,...
|
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...
| |
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...
|
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...
|
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...
|
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...
|
by: adsilva |
last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
|
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
| |
by: muto222 |
last post by:
How can i add a mobile payment intergratation into php mysql website.
| |