473,406 Members | 2,273 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,406 software developers and data experts.

os.path.isdir question

Hello,

I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):
results.remove(a_result)

for x in results: print x

The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?

Thanks,
Mar 16 '08 #1
6 3387
On Mar 15, 8:12 pm, lampshade <mwolff...@gmail.comwrote:
Hello,

I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):
Try if os.path.isdir(os.path.join(my_path, a_result)):
results.remove(a_result)

for x in results: print x

The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?
You should always use os.path.join to join paths. You shouldn't add
them like normal strings. I suspect you're getting a combination which
doesn't exist, so it isn't a dir. :)
>
Thanks,
Mar 16 '08 #2
yoz
lampshade wrote:
Hello,

I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):
results.remove(a_result)

for x in results: print x

The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?

Thanks,
Your code works fine for me ..

Someone will probably complain that this isn't pythonic or hopefully
come up with an even neater way but what about this:

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
files=os.walk(my_path).next()[2]
for x in files: print x

EdH.
Mar 16 '08 #3
On Mar 15, 9:27 pm, Benjamin <musiccomposit...@gmail.comwrote:
On Mar 15, 8:12 pm, lampshade <mwolff...@gmail.comwrote:Hello,
I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.
#!/usr/bin/python
import os
my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):

Try if os.path.isdir(os.path.join(my_path, a_result)): results.remove(a_result)
for x in results: print x
The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?

You should always use os.path.join to join paths. You shouldn't add
them like normal strings. I suspect you're getting a combination which
doesn't exist, so it isn't a dir. :)
Thanks,
Thanks, that nailed it perfectly! I'll remember the os.path.join from
now on!
Mar 16 '08 #4
On Mar 16, 12:12 pm, lampshade <mwolff...@gmail.comwrote:
Hello,

I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.

#!/usr/bin/python
import os

my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):
Not parts of the problem, but:
(1) you don't need to use str() here.
(2) use os.path.join instead of the + operator, if you are interested
in portable code.
results.remove(a_result)

for x in results: print x

The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?

Thanks,
You are removing directory *NAMES* from the container named "results".
If you want to remove the directories from your filesystem, use
os.rmdir.
Mar 16 '08 #5
On Mar 16, 2:27 am, Benjamin <musiccomposit...@gmail.comwrote:
On Mar 15, 8:12 pm, lampshade <mwolff...@gmail.comwrote:Hello,
I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.
#!/usr/bin/python
import os
my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):

Try if os.path.isdir(os.path.join(my_path, a_result)): results.remove(a_result)
for x in results: print x
The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?

You should always use os.path.join to join paths. You shouldn't add
them like normal strings. I suspect you're getting a combination which
doesn't exist, so it isn't a dir. :)
You are also removing items from 'results' while iterating over it,
which has an undefined behaviour. It would be better to build a new
list of those that aren't directories.
Mar 16 '08 #6
On Mar 16, 2:27 pm, MRAB <goo...@mrabarnett.plus.comwrote:
On Mar 16, 2:27 am, Benjamin <musiccomposit...@gmail.comwrote:
On Mar 15, 8:12 pm, lampshade <mwolff...@gmail.comwrote:Hello,
I'm having some problems with os.path.isdir I think it is something
simple that I'm overlooking.
#!/usr/bin/python
import os
my_path = os.path.expanduser("~/pictures/")
print my_path
results = os.listdir(my_path)
for a_result in results:
if os.path.isdir(str(my_path) + str(a_result)):
Try if os.path.isdir(os.path.join(my_path, a_result)): results.remove(a_result)
for x in results: print x
The problem is, that the directories are never removed. Can anyone
point out what I'm missing that is causing the bug? Is there a better
way of doing this?
You should always use os.path.join to join paths. You shouldn't add
them like normal strings. I suspect you're getting a combination which
doesn't exist, so it isn't a dir. :)

You are also removing items from 'results' while iterating over it,
which has an undefined behaviour. It would be better to build a new
list of those that aren't directories.
This list comprehension should do the trick pythonically:
final_results = [a_result for a_result in results if
os.path.isdir(os.path.join(my_path, a_result))]
Mar 17 '08 #7

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

Similar topics

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...
3
by: todd smith | last post by:
having some strange problems with Python2.3 on windowsXP. im trying to access shares and the os.path.isdir function _always_ returns false even though the directories are there and i can os.chdir,...
9
by: Bengt dePaulis | last post by:
I have a local directory that I want to include in my sys.path How to save it permanently? Regards /Bengt
8
by: Egor Bolonev | last post by:
hi all i want my program to ignore ntfs links, but os.path.islink() isnt work as i expect print os.path.islink('''C:\Documents and Settings\åÇÏÒ\My Documents\Scripts\Antiloop\'''') outputs...
4
by: Beman Dawes | last post by:
The docs for os.path.exists(), isdir(), and the like, do not describe behavior when an I/O error occurs. Testing on Windows XP SP2 with Python 2.4.1 (#65, Mar 30 2005, 09:13:57) on win32, on a...
6
by: kimes | last post by:
I've just started digging into how python works.. I found that other mudules are clearly declared like one file per a module.. But the only os.path doesn't have their own file.. ye I know is...
34
by: Reinhold Birkenfeld | last post by:
Hi, the arguments in the previous thread were convincing enough, so I made the Path class inherit from str/unicode again. It still can be found in CVS:...
2
by: Rob Cowie | last post by:
Hi, Given a string representing the path to a file, what is the best way to get at the filename? Does the OS module provide a function to parse the path? or is it acceptable to split the string...
2
by: 7stud | last post by:
Here is a program to print out the files in a directory: ----------- import os myfiles = os.listdir("../") print myfiles for afile in myfiles: print afile
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
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...
0
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,...
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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,...
0
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...

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.