ashok3sep
asked on
Python File Reading and Writing
This is a small program i have written to read a contents from a INPUT FILE containing (+1000 Lines ) and writing simultaneously to another file. ( it works fine )
1.---- QUESTION:-
--------------------
I need to have the same fomat for the written file as of the Reading file ( But the FORMAT is LOST ------ I Dont Know why ?????
2.--------QUESTION:-
-------------------------- -
if want to make a stop at the middle of the file and insert some data to the file and then again perfrom the read and the write operation( HOW CAN I DO THAT)........
import sys
import os
import stat
def ReadFile():
try:
input = open('c:\Abafiles\Job-1.in p','r')
out = open("exampleWrite.inp", "w")
s = input.readlines()
InputTuple = []
for line in s:
singleLine = line.rstrip()
InputTuple.append(singleLi ne)
out.write(singleLine)
print len(InputTuple)
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
input.close()
out.close()
ReadFile()
1.---- QUESTION:-
--------------------
I need to have the same fomat for the written file as of the Reading file ( But the FORMAT is LOST ------ I Dont Know why ?????
2.--------QUESTION:-
--------------------------
if want to make a stop at the middle of the file and insert some data to the file and then again perfrom the read and the write operation( HOW CAN I DO THAT)........
import sys
import os
import stat
def ReadFile():
try:
input = open('c:\Abafiles\Job-1.in
out = open("exampleWrite.inp", "w")
s = input.readlines()
InputTuple = []
for line in s:
singleLine = line.rstrip()
InputTuple.append(singleLi
out.write(singleLine)
print len(InputTuple)
except IOError, (errno, strerror):
print "I/O error(%s): %s" % (errno, strerror)
except ValueError:
print "Could not convert data to an integer."
except:
print "Unexpected error:", sys.exc_info()[0]
raise
input.close()
out.close()
ReadFile()
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
You don't need to know the line number. You can just read the lines one by one and examine them, and decide for each one whether to insert something after it.
For example:
for line in s:
out.write(line)
if INeedToInsertALineAfterThi s(line):
out.write(LineToInsertAfte r(line))
Here INeedToInsertALineAfterThi s and LineToInsertAfter are functions you would have to write. Of course, you don't have to use functions. If the logic is simple enough, you can just stick it in where I have the function calls.
--efn
For example:
for line in s:
out.write(line)
if INeedToInsertALineAfterThi
out.write(LineToInsertAfte
Here INeedToInsertALineAfterThi
--efn
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
The rule of thumb: Never use backlashes in paths in Python. You will do less mistakes when forgetting to double the backslash in strings (like in the question).
ASKER
I need to check for the file for some data.
for example i have a text file "hallo.txt"
######
Hallo.txt
######
1 11 21
2 12 22
3 13 23
4 14 24
5 15 25
6 16 26
7 17 27
8 18 28
Now i need to insert a line 2002 12 22 in the third line and my new file should look like this
1 11 21
2 12 22
2002 12 22
3 13 23
4 14 24
5 15 25
6 16 26
7 17 27
8 18 28
sometimes i need to insert 3003 13 33 after 3rd line and so on........
so i need to check each and every line and then if i find a match then i need to insert a line next to it
how can i find the desired line number from the file and then i need to insert some data to the file and then do the read operation.
Thanks
Freedom