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

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0

    What does this do?


    What does the following python code do?

    PHP Code:
    import time
    file 
    "whatever.txt"
    count 0

    open(file,"r+")
    for 
    line in a:
        
    count+=1
        line
    .strip()
        
    line.lower()
        print 
    "editing line "count

    print "last edited :",  time.asctime()
    a.close() 
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    Code:
    import time 
    file = "whatever.txt" 
    count = 0 #stores the current line of the text file
    
    a = open(file,"r+") #open file for reading 
    for line in a: #cycle through lines in the file
        count+=1 #updates count to indicate the next line
        line.strip() #remove whitespace
                        #should be 'l = line.strip()' to store the return value
        line.lower() #returns the string converted to lowercase. 
                        #should be 'l = line.lower()' to store the return value
        print "editing line ", count #prints the current line
    
    print "last edited :",  time.asctime() #prints the time
    a.close()
    the script really has no final result, it doesn't modify the text file in any way or anything like that. I suppose as a side effect the script tells you how many lines are in a text file.
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    thanks for replying. The problem was I wanted it to clear whitespace and lower() everything, but the file returned nothing modigfied. What was it I was doing wrong?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  6. #4
  7. Hello World :)
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2003
    Location
    Hull, UK
    Posts
    2,537
    Rep Power
    69
    The code effectivly does nothing usful - it iterates over each line in the file and prints the number number - since it doesn't write the modifide lines to the file, so when the program exits, no changes are saved.

    Code:
    #!/usr/bin/env python
    
    path = 'input.txt'
    count = 1
    lines = []
    
    for line in file(path):
        line = line.strip()
        line = line.lower()
        lines.append(line)
        print 'Editing', count
        count = count + 1
    file(path, 'w').writelines(lines)
    Here the lines are appended to the 'lines' list before writing them to the file at the end.

    Note: this is untested code but should work fine.

    Hope this helps,

    Mark.
    Last edited by netytan; August 7th, 2004 at 06:58 AM.
    programming language development: www.netytan.com Hula

  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Intermediate (1500 - 1999 posts)

    Join Date
    Feb 2004
    Location
    London, England
    Posts
    1,585
    Rep Power
    1373
    Originally Posted by netytan
    The code effectivly does nothing usful - it iterates over each line in the file and prints the number number - since it doesn't write the modifide lines to the file, so when the program exits, no changes are saved.

    Code:
    #!/usr/bin/env python
    
    path = 'input.txt'
    count = 1
    lines = []
    
    for line in file(path):
        line = line.strip()
        line = line.lower()
        lines.append(line)
        print 'Editing', count
        count = count + 1
    file(path, 'w').writelines(lines)
    Here the lines are appended to the 'lines' list before writing them to the file at the end.

    Note: this is untested code but should work fine.

    Hope this helps,

    Mark.

    I would suggest closing the first file before opening the second.

    Dave - The Developers' Coach
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    why do you have to assigning and such, isn't there a way to just have it go over the file, modify it and save it? why the need for a making a separate list and putting the .strip() and lower() methods results in that list?
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2004
    Posts
    84
    Rep Power
    11
    Originally Posted by DevCoach
    I would suggest closing the first file before opening the second.

    Dave - The Developers' Coach
    The first file object should be destroyed by Python itself, as there are no longer any references to it once the for loop has finished, at least that is my understanding of it. a call to locals()/globals() after a for loop like that indicates there is no file object in either namespace.

    why do you have to assigning and such, isn't there a way to just have it go over the file, modify it and save it? why the need for a making a separate list and putting the .strip() and lower() methods results in that list?
    you need to open the file for reading and writing separately. modifying the file while you're reading it could cause problems.
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2003
    Location
    Canada
    Posts
    185
    Rep Power
    0
    Originally Posted by rebbit
    you need to open the file for reading and writing separately. modifying the file while you're reading it could cause problems.
    guess that explains why it erased my data. Thanks for all of your replies.
    "In theory, there is no difference between theory and practice.
    But, in practice, there is."

  16. #9
  17. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Dec 2001
    Location
    Houston, TX
    Posts
    383
    Rep Power
    14
    Originally Posted by rebbit
    The first file object should be destroyed by Python itself, as there are no longer any references to it once the for loop has finished, at least that is my understanding of it. a call to locals()/globals() after a for loop like that indicates there is no file object in either namespace.
    You're right, things defined within the for statement are not in the parent scope. Observe.

    Code:
    >>> class A(list): pass
    ...
    >>> for i in A([1, 2, 3, 4]):
    ...     print i
    ...
    1
    2
    3
    4
    >>> locals()
    {'__builtins__': <module '__builtin__' (built-in)>, '__name__': '__main__', 'i': 4, 'A': <class '__main__.A'>, '__doc__': None}
    >>>
    Note that there's no instance of my A class defined, just the class itself.
    Debian - because life's too short for worrying.
    Best. (Python.) IRC bot. ever.

IMN logo majestic logo threadwatch logo seochat tools logo