Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Python3 & os.walk
#1
I am trying to write a script using Python 3 and
os.walk. The code is as follows:

import os

path = '/home/systemconttrol/data_logs'

for root, dirs, files in os.walk(path):
   print(files)
I receive no errors in the shell window.
However after hitting return for the line
'print(files)' nothing happens other than
a blank line is displayed. Hitting enter
a second time will create a display listing
of the log files.

If I run the script from the IDE it simply
returns the cursor to the beginning of the
next line and nothing happens after that.
No repeat tapping of the 'Enter' key causes
any change.

OS used is Ubuntu 18.04
Python version is 3.6.5

Any ideas on how t keep this from wandering
off and staring back at me? Thanks

Wiley
Reply
#2
After you hit <enter> after print(files), the interpreter waits because you may want to enter more lines in the for block. You could add print("hello") after print(files) for example. The for loop cannot be executed until the interpreter is sure that you don't want to append anything to this block. That's why you need to type «enter» a second time to indicate the end of the for block.
Reply
#3
Thanks for the insight. Added 'print('*********') and VOILA!

Danka
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Python & MSGraph - Learning to Walk ITMan020324 2 407 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  EasySNMP Walk/BulkWalk pylance 3 2,075 Nov-29-2021, 12:00 PM
Last Post: pylance
  How to sort os.walk list? Denial 6 11,473 Oct-10-2020, 05:28 AM
Last Post: Denial
  os.walk question DPaul 2 2,314 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk(Path("path_string")) giving error Kumarkv 4 3,851 May-10-2020, 08:46 AM
Last Post: snippsat
  os.walk does not see files that are in the folder kiton 1 3,002 Jan-21-2020, 07:26 PM
Last Post: micseydel
  Gnuradio python3 is not compatible python3 xmlrpc library How Can I Fix İt ? muratoznnnn 3 4,876 Nov-07-2019, 05:47 PM
Last Post: DeaD_EyE
  print notifcation when enter new directory os.walk() evilcode1 3 2,612 Jun-20-2019, 08:19 PM
Last Post: evilcode1
  Animating Random Walk Excelsiscelia 2 5,206 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia
  how to test if an object came from os.walk() Skaperen 3 3,175 Jan-10-2018, 03:05 AM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020