简体   繁体   English

Python os.walk,使用子目录中的文件

[英]Python os.walk, working with files in subdirectories

Learning Python as part of my thesis, and I'm completely self taught/don't know what I'm doing; 学习Python是我的论文的一部分,而且我完全是自学成才/不知道自己在做什么。 apologies if this is trivial. 抱歉,这很简单。 Been scouring the net for a while, and cannot find a "plain english" answer to this, nor can I figure out how to do this on my own. 在网上搜寻了一段时间,无法找到对此的“普通英语”答案,我也无法自行解决如何做到这一点。 I've gleaned many tidbits on knowledge from this site via lurking, so I hope ya'll can help. 我通过潜伏收集了许多有关本站点知识的知识,因此希望您能提供帮助。

I have a folder that contains folders named after years (2001, 2002, 2003, etc), and inside each is a numbered folder for each month (01, 02, 03, etc). 我有一个文件夹,其中包含以年份(2001、2002、2003等)命名的文件夹,并且每个文件夹内每个月都有一个编号的文件夹(01、02、03等)。 Inside each of those are my csv files that I'm working with. 在每个文件中都有我正在使用的csv文件。 I've already set up a program to load them in and analyze them, but it only works when there are no subdirectories involved. 我已经设置了一个程序来加载它们并对其进行分析,但是它仅在不涉及子目录时才起作用。 Otherwise it tells me the file does not exist. 否则,它告诉我该文件不存在。 Simply trying to get python to see the csv files in those subdirectories and run them through the program. 只需尝试让python查看那些子目录中的csv文件并通过程序运行它们即可。

For example, this works just fine: 例如,这很好用:

top = r'C:\Users\Brock\Desktop\Masters_Python&Data\WRF_data\ctl\2006\01'
os.chdir(top)    #change current directory to 'top' object
for root,dirs,files in os.walk(top, topdown = True):
    for file in files:
    #blah blah program goes here

However when I try to run this: 但是,当我尝试运行此命令时:

top = r'C:\Users\Brock\Desktop\Masters_Python&Data\WRF_data\ctl'
os.chdir(top)
for root,dirs,files in os.walk(top, topdown = True):    
    for file in files:
    #blah blah program goes here

It tells me that the first file in the first folder (01) does not exist. 它告诉我第一个文件夹(01)中的第一个文件不存在。 Also throws a random "b" in there after the word "File": 在单词“ File”之后还会在其中抛出一个随机的“ b”:

FileNotFoundError: File b'1136072700_KSUN_wrf6x6.csv' does not exist 

This occurs if top is something like "r'C:\\Users\\Brock\\Desktop\\Masters_Python&Data\\WRF_data\\ctl\\2006' as well. 如果top也类似“ r'C:\\ Users \\ Brock \\ Desktop \\ Masters_Python&Data \\ WRF_data \\ ctl \\ 2006”,则会发生这种情况。

Again, I apologize if this is a stupid question, but I'd rather ask someone who knows rather than me fiddling with it for a week to get somewhere. 再次,我对这是一个愚蠢的问题表示歉意,但我宁愿问一个认识的人,也不要问我一个星期的摆弄。

Cheers 干杯

You need to use glob : 您需要使用glob

import glob
import csv

path = r'C:\Users\Brock\Desktop\Masters_Python&Data\WRF_data\ctl\*\*\*.csv'
for file in glob.iglob(path):
   with open(file) as f:
     reader = csv.reader(f, delimiter=',')
     for row in reader:
         do_something_with(row)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM