Sponsored Content
Full Discussion: Advise on os.path in python
Homework and Emergencies Homework & Coursework Questions Advise on os.path in python Post 302801453 by maverick_here on Thursday 2nd of May 2013 03:52:30 AM
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).
 

7 More Discussions You Might Find Interesting

1. 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

2. 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

3. 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

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

6. 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

7. 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
isympy(1)																 isympy(1)

NAME
isympy - interactive shell for SymPy SYNOPSIS
isympy [-c | --console] isympy [ {-h | --help} | {-v | --version} ] DESCRIPTION
isympy is a Python shell for SymPy. It is just a normal python shell (ipython shell if you have the ipython package installed) that exe- cutes the following commands so that you don't have to: >>> from __future__ import division >>> from sympy import * >>> x, y, z = symbols("xyz") >>> k, m, n = symbols("kmn", integer=True) So starting isympy is equivalent to starting python (or ipython) and executing the above commands by hand. It is intended for easy and quick experimentation with SymPy. For more complicated programs, it is recommended to write a script and import things explicitly (using the "from sympy import sin, log, Symbol, ..." idiom). OPTIONS
-c shell, --console=shell Use the specified shell (python or ipython) as console backend instead of the default one (ipython if present or python otherwise). Example: isympy -c python FILES
${HOME}/.sympy-history Saves the history of commands when using the python shell as backend. BUGS
The upstreams BTS can be found at <http://code.google.com/p/sympy/issues/list> Please report all bugs that you find in there, this will help improve the overall quality of SymPy. SEE ALSO
ipython(1), python(1) 2007-10-8 isympy(1)
All times are GMT -4. The time now is 06:10 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy