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