User avatar
NPDedyukhin
Posts: 21
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: 36098
Joined: Sun Jun 16, 2013 11:19 pm
Location: 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: Having anything humorous in your signature is completely banned on this forum. Wear a tin-foil hat and you'll get a ban.

Any DMs sent on Twitter will be answered next month.

This is a doctor free zone.

User avatar
NPDedyukhin
Posts: 21
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: 5941
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: 21
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: 168
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”