Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
examples using os.walk()
#6
This example?

# !/usr/bin/python3
import os

os.chdir("d:\\tmp")
for root, dirs, files in os.walk(".", topdown = False):
   for name in files:
      print(os.path.join(root, name))
   for name in dirs:
      print(os.path.join(root, name))
I guess a filname or path on your filesystem is using invalid unicode.
The error happens in the function print. You can use a kind of hack to ship aroud this:

def encdec_hack(s):
    return s.encode("utf8", errors="ignore").decode()

for root, dirs, files in os.walk("."):
    for f in files:
        try:
            print("File [ OK  ]:", f)
        except UnicodeEncodeError:
            print()
            print("File [ ERR ]:", encdec_hack(f))
The best solution is: Fix your filesystem. Delete or rename the files with broken encoding.

It can also causes trouble with other Applications like an file explorer, backup tools etc...

BTW: The use of pathlib.Path has the same problem. The representation of the Path is ok
To print the representation of something:
print(repr(something))
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
examples using os.walk() - by Skaperen - Mar-19-2021, 12:28 AM
RE: examples using os.walk() - by Larz60+ - Mar-19-2021, 12:52 AM
RE: examples using os.walk() - by Skaperen - Mar-20-2021, 02:20 AM
RE: examples using os.walk() - by Larz60+ - Mar-20-2021, 02:35 AM
RE: examples using os.walk() - by Skaperen - Mar-20-2021, 05:37 PM
RE: examples using os.walk() - by DeaD_EyE - Mar-20-2021, 06:16 PM
RE: examples using os.walk() - by buran - Mar-20-2021, 06:17 PM
RE: examples using os.walk() - by DeaD_EyE - Mar-20-2021, 06:30 PM
RE: examples using os.walk() - by Skaperen - Mar-21-2021, 10:13 PM
RE: examples using os.walk() - by Skaperen - Mar-21-2021, 10:43 PM
RE: examples using os.walk() - by Skaperen - Mar-22-2021, 01:37 AM
RE: examples using os.walk() - by buran - Mar-22-2021, 06:37 AM
RE: examples using os.walk() - by Skaperen - Mar-22-2021, 05:56 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  features examples by release costa_shul 2 2,609 Sep-06-2020, 11:35 AM
Last Post: costa_shul
  list of compliances of all special methods - examples nzcan 2 2,881 Sep-01-2018, 08:33 PM
Last Post: Windspar
  why i don't like os.walk() Skaperen 20 20,418 Jan-11-2018, 08:39 AM
Last Post: Skaperen
  WSGI working examples Skaperen 1 3,556 May-29-2017, 10:45 AM
Last Post: snippsat

Forum Jump:

User Panel Messages

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