Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9017089
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T04:10:32+00:00 2026-06-16T04:10:32+00:00

i am learning python, and i am having troubles with saving the output of

  • 0

i am learning python, and i am having troubles with saving the output of a small function to file. My python function is the following:

#!/usr/local/bin/python

import subprocess
import codecs

airport = '/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport'

def getAirportInfo():
    arguments = [airport, "--scan" , "--xml"]
    execute = subprocess.Popen(arguments, stdout=subprocess.PIPE)
    out, err = execute.communicate()
    print out
    return out

airportInfo = getAirportInfo()

outFile = codecs.open('wifi-data.txt', 'w')
outFile.write(airportInfo)
outFile.close()

I guess that this would only work on a Mac, as it references some PrivateFrameworks.

Anyways, the code almost works as it should. The print statement prints a huge xml file, that i’d like to store in a file, for future processing. And here start the problems.
In the version above, the script executes without any errors, however, when i try to open the file, i get an error message, along the lines of error with utf-8 encoding. Ignoring this, opens the file, and most of the things look fine, except for a couple of things:

  • some SSID have non-ascii characters, like ä, ö and ü. When printing those on the screen, they are correctly displayed as \xc3\xa4 and so on. When I open the file it is displayed incorrectly, the usual random garbage.

  • some of the xml values look like these when printed on screen: Data(“\x00\x11WLAN-0024FE056185\x01\x08\x82\x84\x8b\x96\x0c\ … x10D\x00\x01\x02”) but like this when read from file: //8AAAAAAAAAAAAAAAAAAA==

I thought it could be an encoding error (seen as the Umlauts have problems, the error message says something about the utf-8 encoding being messed up, and the text containing \x type of characters), and i tried looking here for possible solutions. However, no matter what i do, there are still errors:

  • adding an additional argument ‘utf-8’ to the codecs.open yields a
    UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0x9a in position 24227: ordinal not in range(128) and the generated file is empty.

  • explicitly encoding to utf-8 with outFile.write(airportInfo.encode(‘utf-8’)) before saving results in the same error

  • not being an expert, i tried decoding it, maybe i was just doing the exact opposite of what needed to be done, but i got an UnicodeDecodeError: ‘utf8’ codec can’t decode byte 0x8a in position 8980: invalid start byte

The only the thing that worked (unsurprisingly), was to write the repr() of the string to file, but that is just not what i need, and also i can’t make a nice .plist of a file full with escape symbols.

So please, please, can somebody help me? What am i missing?
If it helps, the type that gets saved in airportInfo is str (as in type(airportInfo) == str) and not u

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T04:10:33+00:00Added an answer on June 16, 2026 at 4:10 am

    You don’t need re-encoding when your text is already unicode. Just write the text to a file. It should just work.

    In [1]: t = 'äïöú'
    
    In [2]: with open('test.txt', 'w') as f:
        f.write(t)
       ...:     
    

    Additionally, you can make getAirportInfo simpler by using subprocess.check_output(). Also, mixed case names should only be used for classes, not functions. See PEP8.

    import subprocess
    
    def get_airport_info():
        args = ['/System/Library/PrivateFrameworks/Apple80211.framework/Versions/Current/Resources/airport', 
                '--scan', '--xml']
        return subprocess.check_output(args)
    
    airportInfo = get_airport_info()
    with open('wifi-data.txt', 'w') as outf:
       outf.write(airportinfo)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Just started learning PySide and is having problem with QTimer I have this #!/usr/bin/python
I have just started learning python version 3 and trying to create a file
I just started using/learning Python and have some questions. I have a text file
I am learning python at the moment and I have a text file with
learning python currently and having a bit of a problem. I'm trying to take
I'm having a great time learning Python, but I've just gotten a bit stuck
I have been learning Python by following some pygame tutorials . Therein I found
I've recently started learning python and am having some trouble. The functions in question
I have the following Python (3.2) code: from pygame import * class Application: def
I'm learning Python, and I'm having trouble with this simple piece of code: a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.