路径的东西 [英] path stuff
问题描述
我正在寻找某个文件名模式的某些目录。
这部分工作正常,但是如果我想要从某个目录中打印某个目录的结果怎么办? ?
例如
d:\dir \mydir1 \ filename.txt< ----------我想
看到这一个
d:\dir \mydir2 \archived\filename.txt< ---------- I我不想
看到存档中的任何内容目录
d:\dir \mydir2 \ filename.txt< ----------再次,我这样做
想要看到这个
我在弄清楚如何使用路径模块时遇到了一些麻烦
来破解路径以确定我是否在我关心的子目录中关于。所以
要么显示某个目录的结果,要么只是简单的
跳过某个目录。
I am walking some directories looking for a certain filename pattern.
This part works fine, but what if I want to exclude results from a
certain directory being printed?
eg
d:\dir\mydir1\filename.txt <----------I want to
see this one
d:\dir\mydir2\archived\filename.txt <----------I don''t want to
see anything in the "archived" directory
d:\dir\mydir2\filename.txt <----------Again, I do
want to see this one
I am having a bit of trouble figuring out how to use the path module
to hack up the path to determine if I am in a subdir I care about. So
either don show me the results from a certain directory or just plain
skip a certain directory.
推荐答案
5月9日下午1点11分,fscked< fsckedag ... @ gmail.comwrote:
On May 9, 1:11 pm, fscked <fsckedag...@gmail.comwrote:
我走路了一些目录寻找某种文件名模式。
这部分工作正常,但如果我想从某个目录中打印某个目录的结果怎么办?
例如
d:\dir \mydir1 \ filename.txt< ----------我想
看到这一个
d:\dir \mydir2 \archived\filename.txt< ----------我不想
查看已存档中的任何内容目录
d:\dir \mydir2 \ filename.txt< ----------再次,我这样做
想要看到这个
我在弄清楚如何使用路径模块时遇到了一些麻烦
来破解路径以确定我是否在我关心的子目录中关于。所以
要么显示某个目录的结果,要么只是简单的
跳过某个目录。
I am walking some directories looking for a certain filename pattern.
This part works fine, but what if I want to exclude results from a
certain directory being printed?
eg
d:\dir\mydir1\filename.txt <----------I want to
see this one
d:\dir\mydir2\archived\filename.txt <----------I don''t want to
see anything in the "archived" directory
d:\dir\mydir2\filename.txt <----------Again, I do
want to see this one
I am having a bit of trouble figuring out how to use the path module
to hack up the path to determine if I am in a subdir I care about. So
either don show me the results from a certain directory or just plain
skip a certain directory.
一种方法是抓住这样的目录路径:
dirPath = os.path.dirname(路径)
然后使用和if:
if'在dirPath中存档:
#跳过此目录
无论如何,这应该让你更接近答案。
Mike
Hi,
One way to do it would be to grab just the directory path like this:
dirPath = os.path.dirname(path)
and then use and if:
if ''archived'' in dirPath:
# skip this directory
That should get you closer to the answer anyway.
Mike
En Wed,2007年5月9日15:11:06 -0300,fscked< fs ********* @ gmail.com>
$ b $bescribió:
En Wed, 09 May 2007 15:11:06 -0300, fscked <fs*********@gmail.com>
escribió:
我正在寻找某个文件名模式的目录。
这部分工作正常,但如果我想从某个目录中打印某个目录的结果怎么办呢?
I am walking some directories looking for a certain filename pattern.
This part works fine, but what if I want to exclude results from a
certain directory being printed?
使用os.walk你可以完全跳过不需要的目录:
for dirpath,dirnames,os.walk中的文件名( starting_dir):
如果归档,在dirnames:
dirnames.remove(" archived")
#进程文件名,通常:
文件名中的文件名:
fullfn = os.path.join(dirpath,filename)
...
-
Gabriel Genellina
Using os.walk you can skip undesired directories entirely:
for dirpath, dirnames, filenames in os.walk(starting_dir):
if "archived" in dirnames:
dirnames.remove("archived")
# process filenames, typically:
for filename in filenames:
fullfn = os.path.join(dirpath, filename)
...
--
Gabriel Genellina
5月9日下午7:02,Gabriel Genellina < gagsl -... @ yahoo.com.arwrote:
On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
En Wed,2007年5月9日15:11:06 -0300,fscked< fsckedag .. 。@ gmail.com
$ b $bescribió:
En Wed, 09 May 2007 15:11:06 -0300, fscked <fsckedag...@gmail.com
escribió:
我正在寻找一些目录寻找某种文件名模式。
这部分工作正常,但如果我想从某个目录中打印某个目录的结果怎么办呢?
I am walking some directories looking for a certain filename pattern.
This part works fine, but what if I want to exclude results from a
certain directory being printed?
使用os.walk你可以完全跳过不需要的目录:
for dirpath,dirnames,os.walk中的文件名( starting_dir):
如果归档,在dirnames:
dirnames.remove(" archived")
#进程文件名,通常:
文件名中的文件名:
fullfn = os.path.join(dirpath,filename)
...
-
Gabriel Genellina
Using os.walk you can skip undesired directories entirely:
for dirpath, dirnames, filenames in os.walk(starting_dir):
if "archived" in dirnames:
dirnames.remove("archived")
# process filenames, typically:
for filename in filenames:
fullfn = os.path.join(dirpath, filename)
...
--
Gabriel Genellina
好吧,这是在Winbloze上它一直给我目录名是
无效:ublahblahblah"到处都是双反斜杠。我现在正在试图找出如何让它们消失的
。我将
稍后再回来查看。
感谢所有帮助到目前为止。 :)
OK, this is on Winbloze and it keeps giving me "The directory name is
invalid: u"blahblahblah" with double backslashies everywhere. I am
currently trying to figure out how to make those go away. I shall
check back in a bit.
thanks for all the help so far. :)
这篇关于路径的东西的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!