×

Discussion Board

Results 1 to 6 of 6
  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    18

    Problems with writing 2 text files

    I used the code below in a python script to open a text file.

    Code:
    import codecs
    
    filename= # file name
    f = codecs.open(filename, 'w', 'utf8')
    
    
    def save():
        f.write('hai)
        f.close()
    NOTE: The above code is just a summary of a much complicated script and it works.

    What should I do in order to open another text file and write another text by using the same script?

  2. #2
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Problems with writing 2 text files

    I'm not sure I understand what you mean, but if you mean just write another text file... that's exactly what you do:
    Code:
    import codecs
    
    filename1= # file name 1
    filename2= # file name 2
    f = codecs.open(filename1, 'w', 'utf8')
    g = codecs.open(filename2, 'w', 'utf8')
    
    
    def save():
        f.write('hai')
        f.close()
        g.write('hai')
        g.close()
    Is that what you meant?

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    18

    Re: Problems with writing 2 text files

    Thanks! But I want to write open a text file, write some texts and save it. And after closing that file, open another text file and do the same. Is it possible?


    Both text files are independent

  4. #4
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Problems with writing 2 text files

    OK, I think I understand now (If not, please excuse me).
    Yes, it's possible. You can open and write as many independent files as you want.

    For example:
    Code:
    import codecs
    
    f=codecs.open("C:\\a.txt", "w", "utf8")
    f.write("some text")
    f.close()
    
    #You can adjust your save function, I'm doing it in general here
    
    #Then, do it for another one
    g=codecs.open("C:\\b.txt", "w", "utf8")
    g.write("some other text")
    g.close()
    Last edited by bogdan.galiceanu; 2008-06-07 at 13:15.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    18

    Re: Problems with writing 2 text files

    Hmmm, thats a nice one.

    But the menu items will be useless.
    Code:
    def save():
        f.write('hai')
        f.close()
        g.write('hai')
        g.close()
    If I use that code, the code above will be unusable. Right?

    Thats because when I open the first file, there'll (g.write('hai')) wont be usable.

  6. #6
    Super Contributor
    Join Date
    Oct 2007
    Location
    Deva, Romania
    Posts
    3,471

    Re: Problems with writing 2 text files

    If you want to put your save functions in the app's menu, you'll have to define a save function for each file or store the active file in a global variable and work with that.
    If not, you can do this:
    Code:
    import codecs
    
    #Define a general save function for all files
    def save(x):
       x.write("hai")
       x.close()
    
    #Now open the first file, work with it and save it
    f=codecs.open("C:\\a.txt", "w", "utf8")
    #...
    save(f)
    
    #Now the second file
    g=codecs.open("C:\\b.txt", "w", "utf8")
    #...
    save(g)
    Last edited by bogdan.galiceanu; 2008-06-07 at 13:47.

Similar Threads

  1. Problems about maemo 4.0.1 SDK installation.
    By fgg_maemo in forum General Development Questions
    Replies: 3
    Last Post: 2008-10-07, 14:26
  2. Replies: 6
    Last Post: 2008-05-04, 02:43
  3. Problems Moving Files with PC Suite
    By marksk in forum General Development Questions
    Replies: 6
    Last Post: 2007-11-27, 09:04
  4. 'hiding' text files
    By serenade in forum Symbian User Interface
    Replies: 4
    Last Post: 2004-05-19, 06:18
  5. Converting SMS files to text files
    By Nokia_Archive in forum General Messaging
    Replies: 1
    Last Post: 2002-05-22, 19:59

Posting Permissions

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