User avatar
NPDedyukhin
Posts: 30
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Writing the result to the list and to a text file.

Thu Oct 10, 2019 3:59 pm

Good afternoon!

There is such a script:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os 
myCmd = 'sudo iwlist wlan0 scan | grep "ESSID"' 
os.system (myCmd)
After executing it, I see the result in the console:
ESSID:"Dedyu"
ESSID:"ROSTELECOM_7F98C"
ESSID:"TP-LINK_9_19"
ESSID:"ROSTELECOM_A0439"
ESSID:"ROSTELECOM_7ED66"
ESSID:"KV-14"
ESSID:"Keenetic_31"
ESSID:"Keenetic-8782"
ESSID:"TP-LINK_DC80"
ESSID:"HomeNet994"
ESSID:"Devlor"
ESSID:"Krat0s's wifi_2"
ESSID:"RTK-104035"
ESSID:"Zinchenko"
ESSID:"Dzirt"
ESSID:"Black Lion"

Please help me edit my script so that it line-by-line saves all this data to a text file.

I tried it this way, but it failed:

Code: Select all

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import os 
myCmd = 'sudo iwlist wlan0 scan | grep "ESSID"' 
networks = [os.system (myCmd)]
with open(r"/home/pi/Desktop/pi_cam/networks.txt", "w") as file:
    for  line in networks:
        file.write(str(line))
After executing such a script, the text file contains only "0" :|
Respectfully,
Nikita Dedyukhin

User avatar
DougieLawson
Posts: 39102
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Writing the result to the list and to a text file.

Thu Oct 10, 2019 4:06 pm

Code: Select all

myCmd = 'sudo iwlist wlan0 scan | grep "ESSID"' 
Change that line to

Code: Select all

myCmd = 'sudo iwlist wlan0 scan | grep "ESSID" > /home/pi/Desktop/pi_cam/networks.txt' 
then delete the rest of the program after the call to os.system().

The whole python piece becomes redundant. You may as well run sudo iwlist wlan0 scan | grep "ESSID" > /home/pi/Desktop/pi_cam/networks.txt in the command line shell, or as a bash shell script.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

User avatar
NPDedyukhin
Posts: 30
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: Writing the result to the list and to a text file.

Thu Oct 10, 2019 4:37 pm

DougieLawson wrote:
Thu Oct 10, 2019 4:06 pm

Code: Select all

myCmd = 'sudo iwlist wlan0 scan | grep "ESSID"' 
Change that line to

Code: Select all

myCmd = 'sudo iwlist wlan0 scan | grep "ESSID" > /home/pi/Desktop/pi_cam/networks.txt' 
then delete the rest of the program after the call to os.system().

The whole python piece becomes redundant. You may as well run sudo iwlist wlan0 scan | grep "ESSID" > /home/pi/Desktop/pi_cam/networks.txt in the command line shell, or as a bash shell script.
Thanks you!
Respectfully,
Nikita Dedyukhin

hippy
Posts: 7714
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Writing the result to the list and to a text file.

Thu Oct 10, 2019 5:00 pm

A more Pythonic way to do it which allows processing of the lines as they are output -

Code: Select all

import os
lines = os.popen('sudo iwlist wlan0 scan | grep "ESSID"').read().strip().split("\n")
with open("/home/pi/Desktop/pi_cam/networks.txt", "w") as f:
  for line in lines:
    f.write(line.strip()+"\n")

User avatar
NPDedyukhin
Posts: 30
Joined: Fri Sep 20, 2019 3:23 am
Location: RU

Re: Writing the result to the list and to a text file.

Fri Oct 11, 2019 4:58 am

hippy wrote:
Thu Oct 10, 2019 5:00 pm
A more Pythonic way to do it which allows processing of the lines as they are output -

Code: Select all

import os
lines = os.popen('sudo iwlist wlan0 scan | grep "ESSID"').read().strip().split("\n")
with open("/home/pi/Desktop/pi_cam/networks.txt", "w") as f:
  for line in lines:
    f.write(line.strip()+"\n")
Thank you very much!
Respectfully,
Nikita Dedyukhin

blimpyway
Posts: 348
Joined: Mon Mar 19, 2018 1:18 pm

Re: Writing the result to the list and to a text file.

Sat Oct 12, 2019 7:40 pm

...or the least pythonic

Code: Select all

#!/bin/bash
sudo iwlist wlan0 scan | grep "ESSID" > /home/pi/Desktop/pi_cam/networks.txt

Return to “Python”