Python Forum
os.walk(Path("path_string")) giving error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
os.walk(Path("path_string")) giving error
#1
Is this correct

dir = "E:\\Books"
p = Path(dir)
reference : https://pbpython.com/pages/mailinglist.html

os.walk(p)
giving below error:

File "F:\Users\hyg\AppData\Local\Programs\Python\Python35\lib\os.py", line 368, in walk
scandir_it = scandir(top)
TypeError: scandir: illegal type for path parameter

However
os.walk(dir)
works fine
Reply
#2
p looks like a pathlib Path, not an os path.
Hard to be sure without full code.
dir = "E:\\Books"
p = Path(dir)
if that is the case, then you can still use os, but you must resolve p first:
os.walk(p.resolve())
better to use a pathlib walk instead (Google: 'pathlib equivalent of os.walk')
Reply
#3
Yes, i have imported pathlib
complete code:

from pathlib import Path 
import os

dir_to_scan = "E:\\Books"
p = Path(dir_to_scan)
q = p / 'jpg'
print(q)

for dirName, subdirList, fileList in os.walk(p):
    print('Found directory: %s' % dirName)
    for fname in fileList: 
        print('\t%s' % fname)
Reply
#4
It does not say what you are importing, but these should both work:

from pathlib import Path
import os

dir = "D:\\Temp2"
path = Path(dir)

for item in os.walk(path):
    print('path',item)

for item in os.walk(dir):
    print('dir',item)
Paul
Reply
#5
(May-10-2020, 06:24 AM)Kumarkv Wrote: TypeError: scandir: illegal type for path parameter
You get this error because you use Python 3.5.
Have to use Python 3.6 or newer for os.walk() to take pathlib object.
You use can it directly like this os.walk(dir_to_scan).
You should of course install a new version Python 3.8 (3.6-3.7) and pip installation under Windows

Then you can also drop the old string formatting %s' % dirName and use f-string.
from pathlib import Path
import os

dir_to_scan = r"C:\code\img"
p = Path(dir_to_scan)
for root, dirs, files in os.walk(p):
    for fname in files:
        if fname.endswith(('.jpg', '.png')):
            print(f'Image found --> {fname}')
Output:
Image found --> nrk.png Image found --> test.jpg
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Smile Python & MSGraph - Learning to Walk ITMan020324 2 354 Feb-04-2024, 04:37 PM
Last Post: ITMan020324
  Compiles Python code with no error but giving out no output - what's wrong with it? pythonflea 6 1,492 Mar-27-2023, 07:38 AM
Last Post: buran
  Script File Failure-Path Error? jerryf 13 3,317 Nov-30-2022, 09:58 AM
Last Post: jerryf
  EasySNMP Walk/BulkWalk pylance 3 2,034 Nov-29-2021, 12:00 PM
Last Post: pylance
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,151 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  How to sort os.walk list? Denial 6 11,298 Oct-10-2020, 05:28 AM
Last Post: Denial
  os.walk question DPaul 2 2,293 May-31-2020, 02:08 PM
Last Post: DPaul
  os.walk does not see files that are in the folder kiton 1 2,968 Jan-21-2020, 07:26 PM
Last Post: micseydel
  print notifcation when enter new directory os.walk() evilcode1 3 2,594 Jun-20-2019, 08:19 PM
Last Post: evilcode1
  Animating Random Walk Excelsiscelia 2 5,175 Nov-24-2018, 08:53 AM
Last Post: Excelsiscelia

Forum Jump:

User Panel Messages

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