walking along and processing files in directory in python

允我心安 提交于 2019-12-01 01:02:45

Addressing Abhisek's comment on Aragon's solution:

import os folder = 'C:'

for root, dirs, files in os.walk(folder):
    for name in files:
        (base, ext) = os.path.splitext(name)
        if ext in "csv":
            print os.path.join(root, name)

you can try this code :

import os
folder = 'C:'

for root, dirs, files in os.walk(folder):
    for name in files:
        print os.path.join(root, name)
    for name in dirs:
        print os.path.join(root, name)

UPDATE:

import os folder = 'C:'

for root, dirs, files in os.walk(folder):
    for name in files:
        nm, ext = os.path.splitext(name)
        if ext == ".csv":
            print os.path.join(root, name)
import os
for (dirpath, dirnames, filenames) in os.walk(directory):
    # Do some processing

That will iterate through the root of the directory specified, for eg. c:/user/name/class/std and enter every folder contained in it, and give you the folders and files contained in them. With that you should be able to do what you need to processing wise.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!