Python Forum
is there equal syntax to "dir /s /b" - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: is there equal syntax to "dir /s /b" (/thread-37968.html)



is there equal syntax to "dir /s /b" - kucingkembar - Aug-16-2022

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


RE: is there equal syntax to "dir /s /b" - Pedroski55 - Aug-16-2022

]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!


RE: is there equal syntax to "dir /s /b" - kucingkembar - Aug-16-2022

(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


This forum uses Lukasz Tkacz MyBB addons.
Forum use Krzysztof "Supryk" Supryczynski addons.