×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    writing text to a file

    hi guys,

    i have this code:
    Code:
    	
    mac_address = (address, services.values()[0])
    if appuifw.query(u"Do you want to always connect to this device?", "query") == True:
    	f = open("c:\\mac_address.txt", 'wb')
    	f.write(mac_address.encode("utf-8"))
    	f.close
    btSocket.connect(mac_address)
    mac_address is a global variable and the bluetooth connects fine, however the code creates the file mac_address.txt but doesn't write anything to the file.

    can anyone see what is wrong?

    thanks

    daven

  2. #2
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: writing text to a file

    mac_address is a tuple, tuples don't have an encode() method.

    What you probably wanted is this:
    Code:
    f.write(str(mac_address).encode('utf-8'))

  3. #3
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: writing text to a file

    ah thanks, i will try that now.

    one question: if i convert mac_address to a string then read it in from the file. can i use it in
    btsocket.connect(mac_address)?

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    386

    Re: writing text to a file

    No, unfortunatelly not.

    But you have several ways to do it.

    The address you want to save consists of a host and a port number. You could store it in file this way:
    Code:
    f.write('%s:%d' % mac_address)
    Reading it would look like this then:
    Code:
    a = f.read().split(':')
    mac_address = (a[0], int(a[1]))
    This way you will get the same tuple you've saved. The split() method in this case returns a list of two strings, the host and the port. The second line make a tuple out of this with the port part converted to int.

    The disadvantage of this method is that it will only work for addresses (host-port tuple).

    A more general way would be to use the marshal module.

    Saving:
    Code:
    marshal.dump(mac_address, f)
    Reading:
    Code:
    mac_address = marshal.load(f)
    The disadvantage of this method is that the file will not be human readable (a binary). Note that because of this you should open the file using "wb" and "rb" modes. Don't use the ".txt" extension too. And don't forget to import the marshal module.

    Hope I helped.

  5. #5
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Re: writing text to a file

    excellent,

    thanks for the help

Similar Threads

  1. How to append text in a text file in j2me
    By lovely_334801 in forum Mobile Java Networking & Messaging & Security
    Replies: 4
    Last Post: 2010-04-26, 11:10
  2. Problem while Reading text file in Nokia3650
    By lalithunt in forum Mobile Java General
    Replies: 5
    Last Post: 2008-07-30, 17:19
  3. Replies: 0
    Last Post: 2004-03-28, 06:51
  4. Replies: 1
    Last Post: 2004-03-14, 21:15
  5. nokia 7210 modem via IR doesn't work !
    By oussamaaiadi in forum PC Suite API and PC Connectivity SDK
    Replies: 1
    Last Post: 2003-03-06, 10:46

Posting Permissions

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