Advise on os.path in python

 
Thread Tools Search this Thread
Homework and Emergencies Homework & Coursework Questions Advise on os.path in python
# 1  
Old 05-02-2013
Advise on os.path in python

Use and complete the template provided. The entire template must be completed. If you don't, your post may be deleted!

1. The problem statement, all variables and given/known data:

I'm writing a python script to give me the number of files and directories in a given directory and I'm having varying results.

I have two scripts which I have written the first one does not work and second one works

I'm using Python 2.4.3 on CentOS 5.9

2. Relevant commands, code, scripts, algorithms:

Code:
#! /usr/bin/python
import os
os.system('clear')

x=raw_input('enter a path ')
print (x)
y=os.listdir(x)
print (y)
k=0
m=0
for a in y:
        if os.path.isfile(a):
                print (a)
                k=k+1
        elif os.path.isdir(a):
                m=m+1
                print (a)


print ('files are %d' % (k))
print ('dirs are %d' % (m))

When I run this I get an output as follows

Code:
[root@#### python]# python os2.py
enter a path /var
/var
files are 0
dirs are 1
[root@##### python]#

3. The attempts at a solution (include all code and scripts):

The following code I have written with few changes works.
Code:
#!/usr/bin/python

import os
os.system('clear')

x=raw_input('enter a path ')
os.chdir(x)  # change made
y=os.listdir('.')
m=0
k=0

for i in y:
        if os.path.isfile(i):
                print i
                m = m + 1
        elif os.path.isdir(i):
                print i
                k = k + 1

print ("%d is the number of files in %s" % (m,x))
print ("%d is the number of directories in %s" % (k,x))

Code:
[root@### python]# python ford.py
enter a path /var
0 is the number of files in /var
25 is the number of directories in /var

4. Complete Name of School (University), City (State), Country, Name of Professor, and Course Number (Link to Course):
Python Programming
Self - learning
Reference The Python Tutorial — Python v2.7.4 documentation

Note: Without school/professor/course information, you will be banned if you post here! You must complete the entire template (not just parts of it).
# 2  
Old 05-06-2013
Solved

The issue is now resolved
Login or Register to Ask a Question

Previous Thread | Next Thread

7 More Discussions You Might Find Interesting

1. Programming

Create a C source and compile inside Python 1.4.0 to 3.7.0 in Python for ALL? platforms...

Hi all... As you know I like making code backwards compatible for as many platforms as possible. This Python script was in fact dedicated for the AMIGA A1200 using Pythons 1.4.0, 1.5.2, 1.6.0, 2.0.1, and 2.4.6 as that is all we have for varying levels of upgrades from a HDD and 4MB FastRam... (1 Reply)
Discussion started by: wisecracker
1 Replies

2. Windows & DOS: Issues & Discussions

How to execute python script on remote with python way..?

Hi all, I am trying to run below python code for connecting remote windows machine from unix to run an python file exist on that remote windows machine.. Below is the code I am trying: #!/usr/bin/env python import wmi c = wmi.WMI("xxxxx", user="xxxx", password="xxxxxxx")... (1 Reply)
Discussion started by: onenessboy
1 Replies

3. UNIX for Advanced & Expert Users

Command to see the logical volume path, device mapper path and its corresponding dm device path

Currently I am using this laborious command lvdisplay | awk '/LV Path/ {p=$3} /LV Name/ {n=$3} /VG Name/ {v=$3} /Block device/ {d=$3; sub(".*:", "/dev/dm-", d); printf "%s\t%s\t%s\n", p, "/dev/mapper/"v"-"n, d}' Would like to know if there is any shorter method to get this mapping of... (2 Replies)
Discussion started by: royalibrahim
2 Replies

4. Shell Programming and Scripting

Pass File name and Directory Path through command to python script

I'm writing python script to get the file-names in the current directory and file sizes .I'm able to get file list and their sizes but unable to pass them through command line. I want to use this script to execute on other directory and pass directory path with file name through command line. Any... (1 Reply)
Discussion started by: etldeveloper
1 Replies

5. Shell Programming and Scripting

**python** unable to read the background color in python

I am working on requirement on spreadsheet in python scripting. I have a spreadsheet containing cell values and with background color. I am able to read the value value but unable to get the background color of that particular cell. Actually my requirement is to read the cell value along... (1 Reply)
Discussion started by: giridhar276
1 Replies

6. UNIX for Dummies Questions & Answers

How to fix Python path for some script/app?

Hello, i have: # python -V Python 2.7.6 But original for my CentOS is 2.3 or 2.4 my python folder: /root/python2.7.6 (inside are folders like lib, include, bin, share) I launched app iotop: # iotop -od 6 Traceback (most recent call last): File "/usr/bin/iotop", line 16, in... (3 Replies)
Discussion started by: postcd
3 Replies

7. Linux

Please advise me.

Hello all, I have a question, and would like some advice please. I am a windows guy by trade....5 years in the Marines is where I learnt a lot of what i know. I took a junior level sys admin job...learned a bit more...and now I do IT security. All of this happened in the last 8 years. So I'm 27... (2 Replies)
Discussion started by: Quality
2 Replies
Login or Register to Ask a Question