How to read in txt file in python 2.6 with Windows op sys

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Avatar19
    New Member
    • Apr 2010
    • 43

    #1

    How to read in txt file in python 2.6 with Windows op sys

    I am unsure how to read in an .txt file in windows with python. I am currently using Python 2.6
    On linux in the terminal you simply type e.g. file.py < inpt.txt , where "file" is the name of the python file, containing any arbitrary program and "inpt" is the name of the text file containing any arbitrary data that the program must manipulate.
    When I program from home I use the "Python GUI" and run my programs in the "Shell".
    Thanks any help on how I can do this would be appreciated, it is not specifically time sensitive but it would help alot to know how to do this from home via Windows XP Professional.
    Appreciate any Help
    Ava19
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    This code will open a file and create a file object, iterate on the file object and save a list of lines from the file that do not contain the word "parrot". It then writes the data back out to the file, overwriting the existing text.
    Code:
    fn = 'text.txt'
    f = open(fn)
    output = []
    for line in f:
        if not 'parrot' in line:
            output.append(line)
    f.close()
    f = open(fn, 'w')
    f.writelines(output)
    f.close()

    Comment

    • Avatar19
      New Member
      • Apr 2010
      • 43

      #3
      Cool thanks bvdet i'll try it
      Ava19

      Comment

      Working...