Python Forum
is there equal syntax to "dir /s /b"
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
is there equal syntax to "dir /s /b"
#1
sorry for my bad English,
currently i looking all files in a folder and subfolder like "dir /s /b" in windows-cmd
D:\test>dir /s /b D:\test\test D:\test\test (2).py D:\test\test.py D:\test\test\test.py
is there any equal syntax like this in python?
Note:
I know code
import os for text in os.listdir(): print(text)
but that only show files name, I need full path
Reply
#2
]Try glob??

import glob path = '/home/pedro/summer2021/OMR/' files = glob.glob(path + '/**', recursive=True) for f in files: # shows all paths and files in path print(f)
or like this:

from pathlib import Path mydir = Path(path) path2python = '/home/pedro/summer2021/OMR/python/' mydir = Path(path2python) filelist = [filename for filename in mydir.iterdir() if filename.is_file()] for filename in filelist: print(f"\nfilename: {filename.name}") print(f"file suffix: {filename.suffix}") print(f"full path: {filename.resolve()}") print(f"filepath parts: {filename.parts}")
or

import os for root,dirs,files in os.walk(path): print('root is:', root) print('dirs is:', dirs) print('files are:', files) for file in files: print(os.path.join(root, file))
If I remember correctly, I got all this from snippsat, so please say thank you to him, not me!
kucingkembar likes this post
Reply
#3
(Aug-16-2022, 07:54 AM)Pedroski55 Wrote: ]Try glob??

import glob path = '/home/pedro/summer2021/OMR/' files = glob.glob(path + '/**', recursive=True) for f in files: # shows all paths and files in path print(f)
or like this:

from pathlib import Path mydir = Path(path) path2python = '/home/pedro/summer2021/OMR/python/' mydir = Path(path2python) filelist = [filename for filename in mydir.iterdir() if filename.is_file()] for filename in filelist: print(f"\nfilename: {filename.name}") print(f"file suffix: {filename.suffix}") print(f"full path: {filename.resolve()}") print(f"filepath parts: {filename.parts}")
or

import os for root,dirs,files in os.walk(path): print('root is:', root) print('dirs is:', dirs) print('files are:', files) for file in files: print(os.path.join(root, file))
If I remember correctly, I got all this from snippsat, so please say thank you to him, not me!

thank you Pedroski55. i will give you reputation point
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Solving equation equal to zero: How to resolve the syntax error? alexfrol86 3 3,813 Feb-21-2022, 08:58 AM
Last Post: deanhystad
  Can a variable equal 2 things? Extra 4 2,990 Jan-18-2022, 09:21 PM
Last Post: Extra
  Not equal a dictionary key value bazcurtis 2 3,174 Dec-11-2019, 11:15 PM
Last Post: bazcurtis
  Misunderstanding with the “if” statement and “not equal” scriptoghost 6 6,566 Jun-23-2017, 09:43 AM
Last Post: DeaD_EyE

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020
This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.