473,769 Members | 2,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

stupid question about os.listdir

OK. I've search on google groups and around the web for this and I
haven't found an answer. I'm a Python newbie and have what I assume is
a basic question. os.listdir takes a pathname as an arg but it doesn't
actually list the contents of the dir I pass in. it always operates on
the current dir (wherever the script is run) and I have to chdir
beforehand. Is that how its supposed to work? If so what is the point
in passing in a pathname?

thanks,

Jason

Jul 18 '05 #1
11 23036
Jason Kratz wrote:
OK. I've search on google groups and around the web for this and I
haven't found an answer. I'm a Python newbie and have what I assume is
a basic question. os.listdir takes a pathname as an arg but it doesn't
actually list the contents of the dir I pass in. it always operates on
the current dir (wherever the script is run) and I have to chdir
beforehand. Is that how its supposed to work? If so what is the point
in passing in a pathname?

thanks,

Jason


oops. almost forgot. if I run interactively in the python interpreter
it works as I expect. its when doing 'python script.py' from the
command line that it only uses the current directory.

Jul 18 '05 #2
Jason Kratz wrote:
oops. almost forgot. if I run interactively in the python
interpreter
it works as I expect. its when doing 'python script.py' from the
command line that it only uses the current directory.


Never heard of any such thing. It's likely that your script is not
quite doing what you expect it to.

--
Erik Max Francis && ma*@alcyone.com && http://www.alcyone.com/max/
__ San Jose, CA, USA && 37 20 N 121 53 W && &tSftDotIotE
/ \ Heaven and Hell / Is on Earth
\__/ Salt-n-Pepa
Jul 18 '05 #3
On Fri, 27 Jun 2003 02:22:36 GMT, Jason Kratz wrote:
os.listdir takes a pathname as an arg but it doesn't actually list the
contents of the dir I pass in.


Please reduce the problem to a simple script that others can examine,
and post it here. If the behaviour is as you say, it should be only a
few lines long:

import os
os.listdir( 'somethingyouth inkshouldwork' )

--
\ "God forbid that any book should be banned. The practice is as |
`\ indefensible as infanticide." -- Dame Rebecca West |
_o__) |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #4
Here is more clarification. Here is my script called backup.py

import os.path
import os

def getdirs(path):

dirs = []

os.chdir(path)
for entry in os.listdir(path ):
if os.path.isdir(e ntry):
dirs.append(ent ry)

return dirs

print getdirs('/')
if I run this from the command line on linux with 'python backup.py' it
works *if* I have os.chdir in there. if I comment it out it doesnt list
starting from the root dir...it starts in my home dir.

If go into the interactive command mode by just typing 'python' at the
prompt and do:

import os
os.listdir('/') then it prints out the dirs under root.

incidentally this happens on windows as well

Jason Kratz wrote:
OK. I've search on google groups and around the web for this and I
haven't found an answer. I'm a Python newbie and have what I assume is
a basic question. os.listdir takes a pathname as an arg but it doesn't
actually list the contents of the dir I pass in. it always operates on
the current dir (wherever the script is run) and I have to chdir
beforehand. Is that how its supposed to work? If so what is the point
in passing in a pathname?

thanks,

Jason


Jul 18 '05 #5
>>>>> "Jason" == Jason Kratz <ea*@joes.com > writes:

Jason> oops. almost forgot. if I run interactively in the python
Jason> interpreter it works as I expect. its when doing 'python
Jason> script.py' from the command line that it only uses the
Jason> current directory.

Code, we need more code. Please post an example, your platform, and
python version.

The following works for me on linux w/ python2.2 called as
python scriptname.py


import os
print os.listdir('/home/jdhunter')
print os.listdir('/home/jdhunter/python')

and the same script (w/ different test paths) works on win32 w/ python
2.2.

JDH

Jul 18 '05 #6
On Fri, 27 Jun 2003 02:57:33 GMT, Jason Kratz wrote:
Here is more clarification. Here is my script called backup.py


Again, I'll ask you to reduce this to a single, isolated incident of
os.listdir() that doesn't act as you expect.

If the failure *depends* on the rest of the script, then it's more
complex than "os.listdir () doesn't list the current directory". It may,
in fact, have nothing to do with os.listdir() at all.

Reducing the test case to a single os.listdir() instance will aid you as
well as us, since you'll be able to have a much better understanding of
what's going on.

--
\ "I was sad because I had no shoes, until I met a man who had no |
`\ feet. So I said, 'Got any shoes you're not using?'" -- Steven |
_o__) Wright |
http://bignose.squidly.org/ 9CFE12B0 791A4267 887F520C B7AC2E51 BD41714B
Jul 18 '05 #7
Ben Finney wrote:
On Fri, 27 Jun 2003 02:22:36 GMT, Jason Kratz wrote:
os.listdir takes a pathname as an arg but it doesn't actually list the
contents of the dir I pass in.

Please reduce the problem to a simple script that others can examine,
and post it here. If the behaviour is as you say, it should be only a
few lines long:

import os
os.listdir( 'somethingyouth inkshouldwork' )


Ben...I tried the above in a new script file (with print os.listdir) and
it works as I thought my other should. Which means i'm doing something
wrong when passing the path in to my function but I'm not sure what. ugh.

Jul 18 '05 #8
Ben Finney wrote:
On Fri, 27 Jun 2003 03:06:04 GMT, Jason Kratz wrote:
Ben Finney wrote:
Please reduce the problem to a simple script


Ben...I tried the above in a new script file (with print os.listdir)
and it works as I thought my other should. Which means i'm doing
something wrong when passing the path in to my function but I'm not
sure what. ugh.

Congratulations ! You've learned an immensely valuable debugging
technique: Reduce the problem behaviour to the *minimum necessary code*
to reproduce the problem; otherwise, you're searhing in code that, it
turns out, has absolutely no bearing on the problem.

(This leads, in turn, to the principle that writing less code in the
first place leads to fewer bugs -- but that will come naturally as you
learn Python :-)


aha! I found it! its the call to os.path.isdir. I'm not passing it
a real pathname....jus t a string. I need to set my entries in my dir
list as real pathnames (ie: with the slashes)...not just the text.
question is how ;)

Jul 18 '05 #9
Jason Kratz wrote:

def getdirs(path):
os.chdir(path)
for entry in os.listdir(path ):
if os.path.isdir(e ntry):
dirs.append(ent ry)

if I run this from the command line on linux with 'python backup.py' it
works *if* I have os.chdir in there. if I comment it out it doesnt list
starting from the root dir...it starts in my home dir.


This might mean you are not passing it an absolute path, but
instead a relative one. Absolute paths (on Linux) are those
which start with a / (forward slash). Anything without that
will start from the current directory only.

But without actual examples of which paths are failing, as
Ben has asked for, we know nothing for certain.

Is it also possible that you are not having a problem with listdir()
at all, but with the values you are passing to os.path.isdir() ?
You realize that os.listdir() returns names that are *relative*
to the path parameter you give it? So that if you then pass those
to isdir() you will get nothing useful if you don't first do
this instead? :

entry = os.path.join(pa th, entry)
if os.path.isdir(e ntry):
dirs.append(ent ry)

-Peter
Jul 18 '05 #10

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

Similar topics

8
2450
by: Hannu Kankaanp?? | last post by:
This may be a bug or simply a strange result of undefined behaviour, but this is what I get with Python 2.3.2 on Windows XP: >>> import os >>> os.listdir('') >>> os.listdir(u'')
0
465
by: Ishwor | last post by:
hi check your seperator variable in the os module. :) for example >>> import os >>> os.sep '\\' Now what you do is :- >> os.listdir("D:" + os.sep + "any_other_folder_name" + os.sep); :)
7
3875
by: Kenneth Pronovici | last post by:
I have some confusion regarding the relationship between locale, os.listdir() and unicode pathnames. I'm running Python 2.3.5 on a Debian system. If it matters, all of the files I'm dealing with are on an ext3 filesystem. The real code this problem comes from takes a configured set of directories to deal with and walks through each of those directories using os.listdir(). Today, I accidentally ran across a directory containing three...
15
2581
by: Riccardo Galli | last post by:
Hi, I noticed that when I use os.listdir I need to work with absolute paths 90% of times. While I can use a for cycle, I'd prefere to use a list comprehension, but it becomes too long. I propose to add an 'abs' keyword which would make os.listdir return the absolute path of files instead of a relative path. This would bring only advantages, I think.
7
1321
by: PullnOutHair | last post by:
I know that I should know how to do this, but for some reason it is escapeing me right now and I was hoping someone here could help. I need to setup a pointer to the memory location 0x30000000. I am working on a linux system that has hardware inputs remapped to that address and I have to read them. I am just drawing a blank on how to actually assign the address of a pointer to point to.
1
3270
by: kai | last post by:
Hello, I use dircache.listdir(myDir) in my module repeatedly. On OS WIN 2000 listdir() will re-read the directory structure! But on AIX, listdir() will not re-read the directory structure (see Python Library Reference). I work with python version 2.2. Now my 2 questions: Why does dircache.listdir() work different?
4
3702
by: quailman | last post by:
Hi all. I'm kind of a python noobie, this is for my first usefull program, beyond just playing around with the language. Any help is appreciated. Here's the code that throws an error. import re, os files = os.listdir(os.getcwd()) #files = def getnum(filename): return float(re.findall(r'\d+',filename)) def numsort(a,b): return cmp(getnum(a),getnum(b))
3
8926
by: vedrandekovic | last post by:
Hello Here is my simple listdir example: Here is my error: WindowsError: The system cannot find the path specified: 'l/ *.*'
0
9424
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10223
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10051
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10000
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8879
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7413
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6675
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5310
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
3
2815
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.