Link to home
Start Free TrialLog in
Avatar of ashok3sep
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.inp','r')
            out = open("exampleWrite.inp", "w")
            s = input.readlines()
            InputTuple = []
            for line in s:
                  singleLine = line.rstrip()
                  InputTuple.append(singleLine)
                  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
Avatar of efn
efn

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of ashok3sep
ashok3sep

ASKER

But i dont know the line number before

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
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 INeedToInsertALineAfterThis(line):
    out.write(LineToInsertAfter(line))

Here INeedToInsertALineAfterThis 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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pepr
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).