how to get a folder name and file name in python -
i have python program named myscript.py
give me list of files , folders in path provided.
import os import sys def get_files_in_directory(path): root, dirs, files in os.walk(path): print(root) print(dirs) print(files) path=sys.argv[1] get_files_in_directory(path)
the path provided d:\python\test
, there folders , sub folder in can see in output provided below :
c:\python34>python myscript.py "d:\python\test" d:\python\test ['d1', 'd2'] [] d:\python\test\d1 ['sd1', 'sd2', 'sd3'] [] d:\python\test\d1\sd1 [] ['f1.bat', 'f2.bat', 'f3.bat'] d:\python\test\d1\sd2 [] ['f1.bat'] d:\python\test\d1\sd3 [] ['f1.bat', 'f2.bat'] d:\python\test\d2 ['sd1', 'sd2'] [] d:\python\test\d2\sd1 [] ['f1.bat', 'f2.bat'] d:\python\test\d2\sd2 [] ['f1.bat']
i need output way :
d1-sd1-f1.bat d1-sd1-f2.bat d1-sd1-f3.bat d1-sd2-f1.bat d1-sd3-f1.bat d1-sd3-f2.bat d2-sd1-f1.bat d2-sd1-f2.bat d2-sd2-f1.bat
how output way.(keep in mind directory structure here example. program should flexible path). how do this. there os command this. can please me solve this? (additional information : using python3.4)
you try using glob
module instead:
import glob glob.glob('d:\python\test\d1\*\*\*.bat')
or, filenames
import os import glob [os.path.basename(x) x in glob.glob('d:\python\test\d1\*\*\*.bat')]
Comments
Post a Comment