#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2013
    Posts
    3
    Rep Power
    0

    Write a program that inputs a text file. The program should print all of the unique w


    Hi,
    below is code
    #!/usr/bin/python

    # Open a file
    fo = open("foo.txt", "w+")
    fo.write( "Python is a great language.\nYeah its great!!\n")

    # Close opend file
    fo.close()
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2013
    Location
    Canada
    Posts
    51
    Rep Power
    2
    Originally Posted by tools
    Hi,
    below is code
    #!/usr/bin/python

    # Open a file
    fo = open("foo.txt", "w+")
    fo.write( "Python is a great language.\nYeah its great!!\n")

    # Close opend file
    fo.close()
    I'm not sure what your question is, but 'w+' mode erases the file that it opens and then allows read/write access. 'r+' allows read/write but does not erase the file.
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,850
    Rep Power
    481

    unique :: set


    Code:
    with open('a.file') as inf:
        data = inf.read()
    print(set(data.split()))
    Untested. My untested code almost never works.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2009
    Posts
    484
    Rep Power
    33
    For starters
    Code:
    with open("/complete/path/file_name", "r") as inp:
        for rec in inp:
            print rec

IMN logo majestic logo threadwatch logo seochat tools logo