473,395 Members | 1,872 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,395 software developers and data experts.

seperating directories from files

raj
i'm surely a newbie in python and on the go, I just tried to write a
small app to change the gnome desktop background daily. It's working
now making use of listdir() to find the contents of each dir I
encounter. But I'm sure that my code won't go that smooth forever.

How can I find if the content is a Directory or not and if it's a file,
whether it is an image file or not?
Is there any method to read the metadata of files?
Thanking all of you in advance.....

Jan 30 '06 #1
5 980
Check out the os.path (http://docs.python.org/lib/module-os.path.html)
module. It has the functions you are looking for.

-adam

Jan 30 '06 #2
For checking the contents of a directory :

for entry in os.listdir(this_dir):
if os.path.isdir(os.path.join(this_dir, entry));
continue

You could use splitext to get the extension of a the filename, and
*assume* that '.jpg', '.png', '.ico' and friends are images.

I'm sure PIL will have ways of checking image metadata (if this is all
you are specifically after here).

All the best,
Fuzzyman
http://www.voidspace.org.uk/python/index.shtml

Jan 30 '06 #3
raj
Thanx adam 'n' fuzzyman! got that 'isdir'. But how the other thing?
Can't find anything suitable. I'll be happy on getting atleast
something like the file command in Unix

Jan 30 '06 #4
raj wrote:
How can I find ... if it's a file,
whether it is an image file or not?
Assuming you are running on an operating system that uses file
extensions to indicate a file is an image (like Windows or Linux),
something like this will work:

import os.path

def isImage(filename):
return os.path.splitext(filename)[1] in ['.jpg','.png','.gif']

(Add as many file extensions to that list as you like...)
Is there any method to read the metadata of files?


It's not what you are looking for here, but os.stat gives you access to
lots of information about files. In Linux and Windows, though, the
'type' of a file is incorporated into its filename through an extension.
Jan 30 '06 #5
> It's not what you are looking for here, but os.stat gives you access to
lots of information about files. In Linux and Windows, though, the
'type' of a file is incorporated into its filename through an extension.


No, only in windows. Under linux/unix in general there might exist the
file-command, which is basically a heuristic approach to the problem by
scanning for magic numbers and the like. Should be installed on any decent
system.

I'm not aware of a wrapping of "file" for python, and as the search terms
are pretty non-descriptive a quick google search hasn't enlightend me. But
you could always go for subprocess and invoke file, of course.

Diez
Jan 30 '06 #6

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

Similar topics

0
by: AnkitAsDeveloper [Ankit] | last post by:
As all we know, in order to remove cyclic includes in C++ we seperate the declarations and definitions of classs and it's member in two files Header (*.h) and source files (*.cpp). This is not a...
4
by: rn5a | last post by:
I have a ListBox which should list all the files & directories that exist in a particular directory. The problem is I can get the ListBox to list either all the files or all the directories but not...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
1
by: rn5a | last post by:
A ListBox lists all the folders & files existing in a directory named 'MyDir' on the server. Assume that the ListBox lists 2 directories - 'Dir1' & 'Dir2' i.e. these 2 directories reside in the...
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: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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?
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
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
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
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...

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.