Results 1 to 3 of 3

Thread: Python file IO

  1. #1
    Join Date
    Jan 2006
    Location
    @ ~/
    Beans
    1,694

    Python file IO

    I am trying to figure out how to do file Input/output,

    I have worked through lots of tutorials online,
    and all the ones that mention file IO only mention the basic howto do it:
    Code:
    file_open = file("bla.txt")
    file_open.write("\n\tbla")
    file_open.close()
    That would open a file (or you can use file_open = open("bla.txt", "w")

    And to read one:
    Code:
    in_file = open("bla.txt","r")
    text = in_file.read()
    in_file.close()

    But, what if i want to open a file and find a specific option, for example,
    if i want to find a specific option in a configuration file, and then just alter its value?

    Or if i want to open a file and save it with the time+date?

    Does anyone have a good example of fileIO in python?


    (sorry for the very lengthy post)

    Chris.
    Using ubuntu offline?
    Want to install new programs?
    Check out wubdepends
    If Stupidity got us into this mess,
    then why can’t it get us out?

  2. #2
    Join Date
    May 2005
    Beans
    Hidden!

    Re: Python file IO

    well, there are many classes builtin to handle config-files like the ConfigParser module, but you could also just read the whole contents to a string object and use the find method, or even the replace method.

    If you wanna rename a file, you could always use something like
    Code:
    import time, os
    
    name= 'myfile'
    
    os.rename(name, '%s-%s' %(name, time.time()))

  3. #3
    Join Date
    Jan 2006
    Location
    @ ~/
    Beans
    1,694

    Re: Python file IO

    Cheers, i shall get back to you if i have any issues
    Using ubuntu offline?
    Want to install new programs?
    Check out wubdepends
    If Stupidity got us into this mess,
    then why can’t it get us out?

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •