473,399 Members | 3,832 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

path stuff

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.

May 9 '07 #1
10 1344
On May 9, 1:11 pm, fscked <fsckedag...@gmail.comwrote:
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.
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

May 9 '07 #2
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?
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

May 10 '07 #3
On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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?

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
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. :)

May 10 '07 #4
On May 10, 10:41 am, fscked <fsckedag...@gmail.comwrote:
On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:


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?
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

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. :)- Hide quoted text -

- Show quoted text -
ok, got the backslashies fixed, not I want it to print just a single
line for each matching filename and dirpath, but it prints 3... hmm...

May 10 '07 #5
On May 10, 12:45 pm, fscked <fsckedag...@gmail.comwrote:
On May 10, 10:41 am, fscked <fsckedag...@gmail.comwrote:


On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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?
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
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. :)- Hide quoted text -
- Show quoted text -

ok, got the backslashies fixed, not I want it to print just a single
line for each matching filename and dirpath, but it prints 3... hmm...- Hide quoted text -

- Show quoted text -
Nevermind, I am indentationally challenged. I was printing under the
for dirpath, dirname, filename part and had to unindent uno time.

It works as desired now, thanks!

May 10 '07 #6
On May 10, 1:43 pm, fscked <fsckedag...@gmail.comwrote:
On May 10, 12:45 pm, fscked <fsckedag...@gmail.comwrote:


On May 10, 10:41 am, fscked <fsckedag...@gmail.comwrote:
On May 9, 7:02 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
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?
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
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. :)- Hide quoted text -
- Show quoted text -
ok, got the backslashies fixed, not I want it to print just a single
line for each matching filename and dirpath, but it prints 3... hmm...-Hide quoted text -
- Show quoted text -

Nevermind, I am indentationally challenged. I was printing under the
for dirpath, dirname, filename part and had to unindent uno time.

It works as desired now, thanks!- Hide quoted text -

- Show quoted text -
ok, I lied, it is still doing the archived folders. Here is the code:

import os, sys
from path import path

myfile = open("boxids.txt", "r", 0)
for line in myfile:
d = 'D:\\Dir\\' + path(line.strip())
for f in d.walkfiles('*Config*.xml'):
for dirpath, dirnames, filenames in os.walk(d):
if "Archived" in dirnames:
dirnames.remove("Archived") #skip this directory
print f
print 'Done'
when it does the print f it still shows me the dirs i don't want to
see.

any more ideas?

TIA

May 10 '07 #7
En Thu, 10 May 2007 19:04:30 -0300, fscked <fs*********@gmail.com>
escribió:
ok, I lied, it is still doing the archived folders. Here is the code:

import os, sys
from path import path

myfile = open("boxids.txt", "r", 0)
for line in myfile:
d = 'D:\\Dir\\' + path(line.strip())
for f in d.walkfiles('*Config*.xml'):
for dirpath, dirnames, filenames in os.walk(d):
if "Archived" in dirnames:
dirnames.remove("Archived") #skip this directory
print f
print 'Done'
when it does the print f it still shows me the dirs i don't want to
see.
You are walking the directory structure *twice*, using two different
methods at the same time. Also, there is no standard `path` module, and
several implementations around, so it would be a good idea to tell us
which one you use.
If you want to omit a directory, and include just filenames matching a
pattern:

import os, sys, os.path, fnmatch

def findinterestingfiles(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
if "Archived" in dirnames:
dirnames.remove("Archived")
for filename in fnmatch.filter(filenames, '*Config*.xml'):
fullfn = os.path.join(dirpath, filename)
print fullfn

myfile = open("boxids.txt", "r")
for line in myfile:
dirname = os.path.join('D:\\Dir\\', line.strip())
findinterestingfiles(dirname):
myfile.close()

--
Gabriel Genellina

May 11 '07 #8
On May 10, 6:08 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
En Thu, 10 May 2007 19:04:30 -0300, fscked <fsckedag...@gmail.com
escribió:


ok, I lied, it is still doing the archived folders. Here is the code:
import os, sys
from path import path
myfile = open("boxids.txt", "r", 0)
for line in myfile:
d = 'D:\\Dir\\' + path(line.strip())
for f in d.walkfiles('*Config*.xml'):
for dirpath, dirnames, filenames in os.walk(d):
if "Archived" in dirnames:
dirnames.remove("Archived") #skip this directory
print f
print 'Done'
when it does the print f it still shows me the dirs i don't want to
see.

You are walking the directory structure *twice*, using two different
methods at the same time. Also, there is no standard `path` module, and
several implementations around, so it would be a good idea to tell us
which one you use.
If you want to omit a directory, and include just filenames matching a
pattern:

import os, sys, os.path, fnmatch

def findinterestingfiles(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
if "Archived" in dirnames:
dirnames.remove("Archived")
for filename in fnmatch.filter(filenames, '*Config*.xml'):
fullfn = os.path.join(dirpath, filename)
print fullfn

myfile = open("boxids.txt", "r")
for line in myfile:
dirname = os.path.join('D:\\Dir\\', line.strip())
findinterestingfiles(dirname):
myfile.close()

--
Gabriel Genellina- Hide quoted text -

- Show quoted text -
Should this code work? I get a syntax error and cannot figure out why.

May 11 '07 #9
En Fri, 11 May 2007 13:25:55 -0300, fscked <fs*********@gmail.com>
escribió:
>import os, sys, os.path, fnmatch

def findinterestingfiles(root_dir):
for dirpath, dirnames, filenames in os.walk(root_dir):
if "Archived" in dirnames:
dirnames.remove("Archived")
for filename in fnmatch.filter(filenames, '*Config*.xml'):
fullfn = os.path.join(dirpath, filename)
print fullfn

myfile = open("boxids.txt", "r")
for line in myfile:
dirname = os.path.join('D:\\Dir\\', line.strip())
findinterestingfiles(dirname):
myfile.close()
Should this code work? I get a syntax error and cannot figure out why.
Sorry, remove the spurious : after findinterestingfiles(dirname) and it
should work fine.

--
Gabriel Genellina

May 12 '07 #10
On May 9, 1:11 pm, fscked <fsckedag...@gmail.comwrote:
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?
You might find this thread helpful

http://tinyurl.com/2guk3l

Note how the backup dirs are excluded.

Highly recommend Python Cookbook, too.

rd

May 12 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: gwen | last post by:
Hi, I have a code to copy/move folders to a specified folder on the root drive - ie c:\stuff However, I also have the folder "stuff" on the root of other partitions as well - ie d:\stuff,...
4
by: Hal Vaughan | last post by:
I want to have a config file for my program, which means I need to know where the config file is. If I type: java myclass and it runs myclass.class, is there any way to obtain the location of...
3
by: Gilles Lenfant | last post by:
Hi, I'm sometimes brain dead for a simple problem like this one : Given an origin absolute URL and a destination absolute URL, I want to make a relative path from the origin to the...
31
by: John Roth | last post by:
I'm adding a thread for comments on Gerrit Holl's pre-pep, which can be found here: http://tinyurl.com/2578q Frankly, I like the idea. It's about time that all of the file and directory stuff...
4
by: Alex | last post by:
Rossum's tutorial states "Actually, modules are searched in the list of directories given by the variable sys.path which is initialized from the directory containing the input script (or the...
2
by: Garry Freemyer | last post by:
I wrote a screensaver, via Visual Studio 2003 in C# and I decided a wiser choice for me was to use an xml file to save my configs. Btw: I wonder if this is why every bit of documentation I've found...
1
by: W1ld0ne [MCSD] | last post by:
In VB6 dlls' I could get the path of the calling web site by using the COM+ class library and the ASP class library. Does anyone have a clue how to do this in VB.Net The objective is to have...
5
by: spike grobstein | last post by:
So, I've got this project I'm working on where the app defines various classes that are subclassed by module packages that act like plugins... I'd like the packages to define a file path for...
5
by: chippy | last post by:
I would like to know how to put an attribute on the root node of the xml returned from a FOR XML PATH query. One thing I tried is this: select m.msgid '@msgID', st.namelong 'set/@namelong',...
34
by: Ben Sizer | last post by:
I've installed several different versions of Python across several different versions of MS Windows, and not a single time was the Python directory or the Scripts subdirectory added to the PATH...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.