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
PYSCRIPT(1)							  [FIXME: manual]						       PYSCRIPT(1)

NOM
pyscript - Pyscript tool for generating an Encapsulated PostScript (EPS) SYNOPSIS
pyscript [--version] pyscript file.py [options] [-o output file name file] [-l log file] DESCRIPTION
Pyscript allows to write images using the Python programming language. Use pyscript to create an Encapsulated PostScript (EPS) from a python program using pyscript objects. An source python file name has to be given to the script. By default, pyscript is written its output to foo.eps where foo.py is the file name of the python program. this behavior can be change by specifying -o output or --output=output to the script. The path of an alternative log file can also be written using the -l file or --logfile=file. OPTIONS
--version/-V print version and URL of the project. --help/-h print usage information --output/-o Output is written to the given file name --logfile/-l Write the logfile to the given file name AUTHORS
Pyscript was written by Alexei Gilchrist aalexei@users.sourceforge.net and Paul Cochrane paultcochrane at users.sourceforge.net. This manual page was written by Arnaud Fontaine arnau@debian.org for the Debian system (but may be used by others). Permission is granted to copy, distribute and/or modify this document under the terms of the GNU General Public License, Version 2 any later version published by the Free Software Foundation. On Debian systems, the complete text of the GNU General Public License can be found in /usr/share/common-licenses/GPL. COPYRIGHT
Copyright (C) 2006 Arnaud Fontaine [FIXME: source] may 05, 2006 PYSCRIPT(1)
All times are GMT -4. The time now is 05:18 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy