Learning Python - Have Question.

A

AlbaClause

I'm just learning Python, and I have a question about os.path.join(dirpath,
name) and its use. Simply put, I haven't figured out how to use it.

I was looking through the Python reference material in the wee hours of the
morning and checking out some of the modules. I was keenly interested in
the os module, as it is necessary for me to learn this stuff in order to
begin my first real Python project.

I was looking at os.walk(top) when I read a little blurb about using
os.path.join(dirpath, name) to get complete directory listings with
path/filname. Unfortunately, I was unable to figure out how to use it. I
kept getting an error with a message something like "b.startswith" or
something like that. I was also unable to find any other information about
this in the reference material.

If someone could offer some insight into the use of os.path.join(dirpath,
name), I would really appreciate it.

Oh, I have one more question. So far everything that I've played with
yields only the filename of file. I am aware that os.walk will place the
pathnames and filenames in a tuple, but I'm wondering if there is a way to
input a full path directory from within Python. Specifically, I want to be
able to get directories like one would get by entering "ls -l" at a *nix
shell prompt.

Thanks.

--
 
I

iapain

I'm just learning Python, and I have a question about os.path.join(dirpath,
name) and its use. Simply put, I haven't figured out how to use it.

First thing you have to remember while using python is "everything is
an object". os.join.path concatenates one or more path for example
os.path.join("c:", "myfolder") represent a path relative to current dir
on c: drive.
I was looking through the Python reference material in the wee hours of the
morning and checking out some of the modules. I was keenly interested in
the os module, as it is necessary for me to learn this stuff in order to
begin my first real Python project.

Dont worry Python is quite easy to learn, just keep on coding in
python.
Oh, I have one more question. So far everything that I've played with
yields only the filename of file. I am aware that os.walk will place the
pathnames and filenames in a tuple, but I'm wondering if there is a way to
input a full path directory from within Python. Specifically, I want to be
able to get directories like one would get by entering "ls -l" at a *nix
shell prompt.

you could easily do it with python. Its more than your expectation. The
best would be to call os.system(shell cmd). You can also print tuple in
your own way.

Cheers!
Deepak
 
A

AlbaClause

iapain said:
First thing you have to remember while using python is "everything is
an object". os.join.path concatenates one or more path for example
os.path.join("c:", "myfolder") represent a path relative to current dir
on c: drive.

Thank you. I was thinking about it all wrong. That helped a lot. I have
it figured out now. :)



--
 
T

Tal Einat

iapain said:
First thing you have to remember while using python is "everything is
an object". os.join.path concatenates one or more path for example
os.path.join("c:", "myfolder") represent a path relative to current dir
on c: drive.

Actually, os.path.join() is a simple function, there's nothing
Obejct-Oriented about it. "Everything is an object" simply means that
functions are objects, but that doesn't mean that the design of
everything is Object-Oriented.
you could easily do it with python. Its more than your expectation. The
best would be to call os.system(shell cmd).

Using the shell for this is the least cross-platform solution possible.
Python's libraries have very corss-platform implementations of such
operations you should use them!

In this case, it seems you're looking for os.listdir(dir_name). Just
call it with the path of a directory and it will return a list of names
of all the files and directories therein. You can use os.path.isdir(),
os.path.isfile(), etc. to figure out what type of file system object
each item in the list is. You can check premissions for the current
user with os.access(). Finally, you can find out more data about any
file with the "stat" module, which is more low-level.

- Tal Einat
reduce(lambda m,x:[m+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]
 
A

AlbaClause

Tal Einat wrote:
Actually, os.path.join() is a simple function, there's nothing
Obejct-Oriented about it. "Everything is an object" simply means that
functions are objects, but that doesn't mean that the design of
everything is Object-Oriented.

It turns out that I was using os.path.join() properly in the first place.
And you're right, it is a very simple function. Apparently, I was using
os.walk() improperly. More specifically, I was not fully understanding how
os.walk() worked. I've played with it some more in the Python interpreter
and I now have a very good understanding of how it works.

For example:

import os
d=os.walk('/server1')
for a, b, c in d:
pass

This will place the path '/server1' in 'a', all directories in 'a' in a list
called 'b', and all files in a list called 'c'. On the next iteration of
the loop, 'a' will contain the path '/server1' with the path of the first
directory in '/server1' appended. So 'a' will be '/server1/directory1',
all of the directories in 'a' will be stored in 'b', and all of the files
in 'a' will be stored in 'c'.

So on the first iteration, a, b, and c would be:

a = server1
b = directory1, directory2, directory3
c = file1, file2, file3

On the second iteration of the loop, a, b, and c would be:

a = server1/directory1
b = subdirectory1, subdirectory2
c = d1file1, d1file2, d1file3

On the third iteration of the loop, a, b, and c would be:

a = server1/directory1/subdirectory1
b = [] # there are no directories in subdirectory1 so b is empty.
c = sd1file1, sdfile2

Using the shell for this is the least cross-platform solution possible.
Python's libraries have very corss-platform implementations of such
operations you should use them!

In this case, it seems you're looking for os.listdir(dir_name). Just
call it with the path of a directory and it will return a list of names
of all the files and directories therein. You can use os.path.isdir(),
os.path.isfile(), etc. to figure out what type of file system object
each item in the list is. You can check premissions for the current
user with os.access(). Finally, you can find out more data about any
file with the "stat" module, which is more low-level.

Thank you for this. The most daunting task in learning Python, is learning
all of the modules and functions that are available. And there's a tonne
of them. :)

--
 
T

Tal Einat

Thank you for this. The most daunting task in learning Python, is learning
all of the modules and functions that are available. And there's a tonne
of them. :)
Actually, much of this file-system related stuff really is badly spread
out between many different modules (os, os.path, glob, fnmatch, shutil,
stat), mostly for historical reasons. Consulting the docs often and
viewing working code examples are a good way to learn how to
effectively do these things with Python's current stdlib.

Some discussion has been going on for several years now about joining
all such functionality into one "path" module, by creating a standard
Path object, and having all of this nicely Object-Oriented. Currently
there is a PEP about this (see PEP 355), but with the release of Python
2.5 nearing this has been further delayed by the development community.

PEP 355: http://www.python.org/dev/peps/pep-0355/

- Tal
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top