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

Learning Python - Have Question.

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.

--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

Aug 26 '06 #1
5 1669
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

Aug 26 '06 #2
iapain wrote:
>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.
Thank you. I was thinking about it all wrong. That helped a lot. I have
it figured out now. :-)

--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

Aug 26 '06 #3

iapain wrote:
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.
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.
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).
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[i]+s[-1] for i,s in enumerate(sorted(m))],
[[chr(154-ord(c)) for c in '.&-&,l.Z95193+179-']]*18)[3]

Aug 27 '06 #4
Tal Einat wrote:
iapain wrote:
>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.
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

>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.
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. :-)

--
--
There are several things that I will never be:
* I will never be attracted to females.
* I will never enjoy the company of others.
Exactly how these realities bode for my enemy, is not of my concern.

Aug 27 '06 #5
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

Aug 28 '06 #6

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

Similar topics

5
by: Ron Stephens | last post by:
The newly rechristened Python Learning Foundation is a web site dedicated to the assistance of people learning the Python programming language. Features include: 1. Daily lists of new and recent...
6
by: post400 | last post by:
Hi , I was just wondering ( yeah I know it's not the first time this question pops up ) what would be the best 2 or 3 books for someone who wants to learn Python , already experienced in other...
4
by: Ray | last post by:
I want to jump in a learn Python. I have spent about a day looking at editors and IDEs and (probably prematurely) selected jEdit to work in. I have downloaded Python and jEdit. I have been going...
2
by: AnOvercomer02 | last post by:
Hi. What is the best way to learn Python? None of the local schools near me teach any courses on the topic. Thanks. -- Cody Houston AnOvercomer02@webtv.net
7
by: jeffbernstein | last post by:
Greetings. I'm reading "How to think like a computer scientist: Learning with Python" and there's a question regarding string operations. The question is, "Can you think of a property that...
15
by: John Salerno | last post by:
After my last post, I thought of another question as a result of the following: ------------------------------ Mike Meyer wrote: > John Salerno <johnjsal@NOSPAMgmail.com> writes: > > >>So...
14
by: Rich | last post by:
Hi, (this is a probably a bit OT here, but comp.lang seems rather desolated, so I'm not sure I would get an answer there. And right now I'm in the middle of learning Python anyway so...) ...
5
by: romiro | last post by:
Hi all, I'm a PHP5 developer looking to "broaden my horizons" so to speak by learning a new language. I emphasize the 5 in PHP since I have fully engrossed myself in the full OOP of version 5...
16
by: John Salerno | last post by:
Just something that crosses my mind every time I delve into "Learning Python" each night. Does anyone see any value in learning Python when you don't need to for school, work, or any other reason?...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...
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,...

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.