I've struggled with this for quite a while and I'm am just not sure
what is going on. I have the following code
import os
def buildList( directory='/Users/mkonrad' )
dirs = [ ]
listing = os.listdir(directory)
for x in listing:
if os.path.isdir(x):
dirs.append(x)
return dirs
This always returns an empty list.
Now if I change it so that directory='.' or directory=os.getcwd()
Then it returns a list of directories.
Any ideas?
Thank you,
-Michael 19 3253
"SU News Server" <mk*****@syr.edu> wrote: I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os
def buildList( directory='/Users/mkonrad' )
dirs = [ ]
listing = os.listdir(directory)
for x in listing: if os.path.isdir(x): dirs.append(x)
return dirs
This always returns an empty list. Now if I change it so that directory='.' or directory=os.getcwd() Then it returns a list of directories.
hint: if you're not sure what's going on in your program, adding
a print statement or two is an easy way to figure it out. like, say:
for x in listing:
print x
if os.path.isdir(x):
dirs.append(x)
(try this before you continue)
:
:
:
the problem is that os.listdir returns a list of filenames, without
the preceeding directory name. you can add an os.path.join
for x in listing:
x = os.path.join(directory, x)
if os.path.isdir(x):
dirs.append(x)
or use the glob module instead:
listing = glob.glob(os.path.join(directory, "*"))
hope this helps!
</F>
On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
Try passing the full pathname of each item to os.path.isdir()
You can create the pathname using os.path.join(directory, x)
--
Richard
"Fredrik Lundh" <fr*****@pythonware.com> wrote: "SU News Server" <mk*****@syr.edu> wrote:
I've struggled with this for quite a while and I'm am just not sure what is going on. I have the following code import os
def buildList( directory='/Users/mkonrad' )
dirs = [ ]
listing = os.listdir(directory)
for x in listing: if os.path.isdir(x): dirs.append(x)
return dirs
This always returns an empty list. Now if I change it so that directory='.' or directory=os.getcwd() Then it returns a list of directories. hint: if you're not sure what's going on in your program, adding a print statement or two is an easy way to figure it out. like, say:
for x in listing: print x if os.path.isdir(x): dirs.append(x)
Did that and I was just getting a bunch of [ ].
(try this before you continue)
: : :
the problem is that os.listdir returns a list of filenames, without the preceeding directory name. you can add an os.path.join
for x in listing: x = os.path.join(directory, x) if os.path.isdir(x): dirs.append(x)
OK. This works except each entry in dirs now includes the full path.
Is there an unjoin? :) I haven't spent any time trying to work this out. or use the glob module instead:
listing = glob.glob(os.path.join(directory, "*"))
hope this helps!
</F>
Richard Townsend <ri*****@nospam.com> wrote: On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
Try passing the full pathname of each item to os.path.isdir()
You can create the pathname using os.path.join(directory, x)
I wonder if I can join ./, so I don't have the full path in each
entry?
Thank you for responding.
_Michael
On 11 Nov 2005 22:00:04 GMT, Michael Konrad wrote: Richard Townsend <ri*****@nospam.com> wrote:
On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
Try passing the full pathname of each item to os.path.isdir()
You can create the pathname using os.path.join(directory, x)
I wonder if I can join ./, so I don't have the full path in each entry?
Thank you for responding. _Michael
Why not assign the os.path.join() result to a new variable, pass that to
os.path.isdir() but still append x to the list?
--
Richard
Michael Konrad wrote: for x in listing: print x if os.path.isdir(x): dirs.append(x)
Did that and I was just getting a bunch of [ ].
if you printed "x" (the filename), that doesn't sound very likely.
maybe you printed some other variable? (like "dirs") for x in listing: x = os.path.join(directory, x) if os.path.isdir(x): dirs.append(x)
OK. This works except each entry in dirs now includes the full path. Is there an unjoin? :)
use two variables:
for name in listing:
fullname = os.path.join(directory, name)
if os.path.isdir(fullname):
dirs.append(name)
</F>
On 11 Nov 2005 22:00:04 GMT, Michael Konrad <mk*****@syr.edu> wrote: Richard Townsend <ri*****@nospam.com> wrote:
On 11 Nov 2005 21:20:33 GMT, SU News Server wrote:
Try passing the full pathname of each item to os.path.isdir()
You can create the pathname using os.path.join(directory, x)
I wonder if I can join ./, so I don't have the full path in each entry?
Thank you for responding. _Michael
-- http://mail.python.org/mailman/listinfo/python-list
I tried this and no error reported but nothing appear on the console, why?
import os
def buildList( directory='c:\TEMP' ):
dirs = [ ]
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
print x
if os.path.isdir(x):
dirs.append(x)
return dirs
Shi Mu wrote: I tried this and no error reported but nothing appear on the console, why?
import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
is that the entire script? you're defining a function, but you're
not calling it. try adding
print buildList()
at the end of the script.
</F>
On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote: Shi Mu wrote:
I tried this and no error reported but nothing appear on the console, why?
import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
is that the entire script? you're defining a function, but you're not calling it. try adding
print buildList()
at the end of the script.
It works but i am curious why the line of "print x" does not show
anything. many thanks!
"Shi Mu" wrote: but i am curious why the line of "print x" does not show anything.
because your c:\temp directory is empty ?
</F>
On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote: "Shi Mu" wrote:
but i am curious why the line of "print x" does not show anything.
because your c:\temp directory is empty ?
</F>
print buildList() gets lots of stuffs from my temp directory(there do
exist lots of files).
But why "print x' has nothing?
"Shi Mu" wrote: print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py
import os
def buildList( directory='c:\TEMP' ):
dirs = [ ]
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
print x
if os.path.isdir(x):
dirs.append(x)
return dirs
print buildList()
C:\>dir temp
....
2005-11-12 00:00 <KAT> .
2005-11-12 00:00 <KAT> ..
2005-11-12 00:00 20 bacon.dat
2005-11-12 00:00 <KAT> egg
2005-11-12 00:00 20 spam.txt
2 fil(er) 40 byte
3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py
c:\TEMP\bacon.dat
c:\TEMP\egg
c:\TEMP\spam.txt
['c:\\TEMP\\egg']
</F>
Shi Mu wrote: On 11/11/05, Fredrik Lundh <fr*****@pythonware.com> wrote:Shi Mu wrote:def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
It works but i am curious why the line of "print x" does not show anything. many thanks!
Did you use directory='c:\TEMP' as shown above, or directory='c:\temp' ?
If you used the lower case version, you are not really checking the
temp directory, since \t represents a TAB character. If that's the
case, try using a forward slash instead: c:/temp .
-Peter
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(directory)
#get just the directories
for x in listing:
if os.path.isdir(directory+os.sep+x):
dirs.append(x)
return dirs
Fredrik Lundh wrote: "Shi Mu" wrote:
print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
print buildList()
C:\>dir temp ...
2005-11-12 00:00 <KAT> . 2005-11-12 00:00 <KAT> .. 2005-11-12 00:00 20 bacon.dat 2005-11-12 00:00 <KAT> egg 2005-11-12 00:00 20 spam.txt 2 fil(er) 40 byte 3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py c:\TEMP\bacon.dat c:\TEMP\egg c:\TEMP\spam.txt ['c:\\TEMP\\egg']
</F>
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(directory)
#get just the directories
for x in listing:
if os.path.isdir(directory+os.sep+x):
dirs.append(x)
return dirs
Fredrik Lundh wrote: "Shi Mu" wrote:
print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
print buildList()
C:\>dir temp ...
2005-11-12 00:00 <KAT> . 2005-11-12 00:00 <KAT> .. 2005-11-12 00:00 20 bacon.dat 2005-11-12 00:00 <KAT> egg 2005-11-12 00:00 20 spam.txt 2 fil(er) 40 byte 3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py c:\TEMP\bacon.dat c:\TEMP\egg c:\TEMP\spam.txt ['c:\\TEMP\\egg']
</F>
This is what I decided on for a solution. I haven't tested it
cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'):
"""Returns a list of directories."""
#variables
dirs = [] #list of directories
#list of directories and files
listing = os.listdir(directory)
#get just the directories
for x in listing:
if os.path.isdir(directory+os.sep+x):
dirs.append(x)
return dirs
Fredrik Lundh wrote: "Shi Mu" wrote:
print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
print buildList()
C:\>dir temp ...
2005-11-12 00:00 <KAT> . 2005-11-12 00:00 <KAT> .. 2005-11-12 00:00 20 bacon.dat 2005-11-12 00:00 <KAT> egg 2005-11-12 00:00 20 spam.txt 2 fil(er) 40 byte 3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py c:\TEMP\bacon.dat c:\TEMP\egg c:\TEMP\spam.txt ['c:\\TEMP\\egg']
</F>
Sorry about that, I guess send was working.
Michael Konrad wrote: This is what I decided on for a solution. I haven't tested it cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'): """Returns a list of directories.""" #variables dirs = [] #list of directories
#list of directories and files listing = os.listdir(directory)
#get just the directories for x in listing: if os.path.isdir(directory+os.sep+x): dirs.append(x)
return dirs
Fredrik Lundh wrote: "Shi Mu" wrote:
print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
print buildList()
C:\>dir temp ...
2005-11-12 00:00 <KAT> . 2005-11-12 00:00 <KAT> .. 2005-11-12 00:00 20 bacon.dat 2005-11-12 00:00 <KAT> egg 2005-11-12 00:00 20 spam.txt 2 fil(er) 40 byte 3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py c:\TEMP\bacon.dat c:\TEMP\egg c:\TEMP\spam.txt ['c:\\TEMP\\egg']
</F>
Shi Mu:
Before all you were doing was defining a function with:
import os
def buildList( directory='c:\TEMP' ):
dirs = [ ]
listing = os.listdir(directory)
for x in listing:
x = os.path.join(directory, x)
print x
if os.path.isdir(x):
dirs.append(x)
return dirs
when you python this file, it does not execute the function, it only
defines it.
Later Lundh told you to add:
print buildList()
to the end of the file. Not only does this execute buildList() but it
also prints out the list "dirs" that buildList returns. So the first
time it wasn't that "print x" wasn't printing anything, it was only
that you weren't executing the function buildList(). If, at the end of
the file, you put buildList() you will only see output values
corresponding to the print x statement
thanks a lot!
On 11/11/05, Michael Konrad <mk*****@syr.edu> wrote: This is what I decided on for a solution. I haven't tested it cross-platform yet.
import os
def dirListing(directory='/Users/mkonrad'): """Returns a list of directories.""" #variables dirs = [] #list of directories
#list of directories and files listing = os.listdir(directory)
#get just the directories for x in listing: if os.path.isdir(directory+os.sep+x): dirs.append(x)
return dirs
Fredrik Lundh wrote: "Shi Mu" wrote:
print buildList() gets lots of stuffs from my temp directory(there do exist lots of files). But why "print x' has nothing?
C:\>more script.py import os
def buildList( directory='c:\TEMP' ): dirs = [ ] listing = os.listdir(directory) for x in listing: x = os.path.join(directory, x) print x if os.path.isdir(x): dirs.append(x) return dirs
print buildList()
C:\>dir temp ...
2005-11-12 00:00 <KAT> . 2005-11-12 00:00 <KAT> .. 2005-11-12 00:00 20 bacon.dat 2005-11-12 00:00 <KAT> egg 2005-11-12 00:00 20 spam.txt 2 fil(er) 40 byte 3 katalog(er) 9 818 021 888 byte ledigt
C:\>python script.py c:\TEMP\bacon.dat c:\TEMP\egg c:\TEMP\spam.txt ['c:\\TEMP\\egg']
</F>
-- http://mail.python.org/mailman/listinfo/python-list This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Dean |
last post by:
Hi
I've got a question relating to using Javascript on an Intranet. I
have a directory with a list of files in the format week36.xls,
week37.xls
and I want to write a script that will scan...
|
by: Tom |
last post by:
I need to get a directory listing through http. If I put the directory
path in the browser address bar such as http://somewebpage.com/subdir
I get the listing of the directory. Of course this is...
|
by: gil |
last post by:
Is it possible to prevent a browser from listing the entire contents of
a folder?
The site, is hosted on my ISP with the following layout-
site/ "user name from ISP"
pagefile (dir)...
|
by: ngr |
last post by:
I used to use the DirListox control in VB6 but this is no longer supported
in VB.NET.
What I want to do:
I want to take a directory listing and show any subdirectories in a list.
When you click...
|
by: dougawells |
last post by:
Hi -
I'm hoping for help with the auto-generation of a hyperlinked listing
of all files in a directory. The server I use does not auto-generate
this. So, when someone comes to this directory and...
|
by: epikto |
last post by:
I have a mapped share that I am trying to get a listing of all the files that it contains.
I use the following code to access the contents
String files = Directory.GetFiles(path);
I can then...
|
by: techusky |
last post by:
I have a *very* simple script written that displays the directory
listing of the current working directory, but I am having some
difficulty when I try to change folders. Basically, I have my $dir...
|
by: marcusadeleon |
last post by:
Hi,
I was wondering how to open up a web directory to get a file listing.
The directory I want to open allows directory listings, but has a
directory password. Meaning it gives me a prompt to...
|
by: Steve |
last post by:
My site is hosted on a Godaddy reseller site. Godaddy only allows the
use of Curl to access remote sites.
What is the method for listing a directory after connecting to the
site with Curl?
...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: giovanniandrean |
last post by:
The energy model is structured as follows and uses excel sheets to give input data:
1-Utility.py contains all the functions needed to calculate the variables and other minor things (mentions...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be using a very simple database which has Form (clsForm) & Report (clsReport) classes that simply handle making the calling Form invisible until the Form, or all...
|
by: Teri B |
last post by:
Hi, I have created a sub-form Roles. In my course form the user selects the roles assigned to the course.
0ne-to-many. One course many roles.
Then I created a report based on the Course form and...
|
by: nia12 |
last post by:
Hi there,
I am very new to Access so apologies if any of this is obvious/not clear.
I am creating a data collection tool for health care employees to complete. It consists of a number of...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
| |