[Tutor] Getting "file sizes"

Joshua Banks syn_ack at comcast.net
Wed Feb 18 11:00:44 EST 2004


-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Hello,

I'm trying to get python to tell me the size of each individual file 
that if finds in a given directory. I'm a little lost on how to get 
that info.

I've done the following:

>>> import os, os.path

#Assign a user friendly name and "path" for variable value. 
#Get a list of files in a given directory

>>> portage_dir = "/var/log/portage"

>>> os.listdir(portage_dir)
['1782-openmotif-2.1.30-r3.log', '1781-iptables-1.2.8-r1.log', 
'1756-slocate-2.7-r5.log', '1763-xloadimage-4.1-r1.log', 
'1773-iproute-20010824-r4.log', '1757-gentoo-sources-2.4.22-r5.log', 
'1788-tcl-8.3.4.log', '1797-libperl-5.8.0.log', 
'1769-python-2.3.3.log', '1776-xfree-drm-4.3.0-r6.log', 
'1806-ymessenger-1.0.4.1.log', '1766-wine-20031118-r1.log', 
'1800-mirrorselect-0.82-r3.log', '1746-kdeaddons-3.1.5.log', 
'1783-gaim-0.72-r1.log', '1752-kdeutils-3.1.5.log', 
'1766-xv-3.10a-r3.log', '1775-xfree-drm-4.3.0-r7.log', 
'1798-perl-5.8.0-r12.log', '1753-kdeartwork-3.1.5.log', 
'1757-slocate-2.7-r2.log', '1766-fileutils-4.1.11-r1.log', 
'1766-yacc-1.9.1-r1.log', '1792-libxml2-2.6.3.log', 
'1766-commonbox-styles-0.6.log', '1767-cryptplug-0.3.15.log', 
'1753-kdeutils-3.1.4.log', '1748-kdepim-3.1.4.log', 
'1791-python-fchksum-1.6.1-r1.log', '1803-gaim-0.75-r8.log', 
'1777-man-pages-1.65.log', '1783-gaim-encryption-2.21.log', 
'1774-iptables-1.2.8-r1.log', '1762-nmap-3.48.log', 
'1808-curl-7.10.8-r1.log', '1804-gaim-encryption-2.21.log', 
'1766-gpgme-0.3.14.log', '1810-bison-1.35.log', 
'1771-libvorbis-1.0.1.log', '1779-xpdf-2.03.log', 
'1766-ymessenger-1.0.4.1.log', '1766-xinetd-2.3.12.log', 
'1759-genkernel-3.0.1_beta6.log', '1766-modutils-2.4.25.log', 
'1778-man-pages-1.64.log', '1765-kgpg-1.0.0.log', 
'1784-kbd-1.08-r5.log', '1751-kdeadmin-3.1.5.log', 
'1801-gaim-0.75-r8.log', '1782-gaim-0.75-r8.log', 
'1787-genkernel-3.0.1_beta8.log', '1763-nmap-3.45.log', 
'1801-mirrorselect-0.82-r2.log', '1754-kdeartwork-3.1.4.log', 
'1766-addpatches-0.2.log', '1809-bison-1.875.log', 
'1795-libvorbis-1.0.1-r1.log', '1766-rpm2targz-9.0-r1.log', 
'1776-java-config-1.2.5.log', '1766-commonbox-utils-0.4.log', 
'1747-kdeaddons-3.1.4.log', '1780-xpdf-2.02.1.log', 
'1796-libperl-5.8.2.log', '1761-file-4.06.log', 
'1764-gnupg-1.2.3-r5.log', '1805-rpm2targz-9.0-r2.log', 
'1795-linux-headers-2.4.21.log', '1752-kdeadmin-3.1.4.log', 
'1786-genkernel-3.0.1_beta6.log', '1786-genkernel-3.0.1_beta9.log', 
'1758-xfree-drm-4.3.0-r6.log', '1794-libvorbis-1.0.1-r2.log', 
'1777-java-config-1.2.4.log', '1771-genkernel-3.0.1_beta6.log', 
'1756-fluxbox-0.9.7.log', '1770-libvorbis-1.0.1-r1.log', 
'1766-libsndfile-1.0.5.log', '1750-kdegraphics-3.1.5.log', 
'1780-iptables-1.2.9.log', '1762-file-4.02.log', '1799-nmap-3.50.log', 
'1772-gentoo-sources-2.4.22-r5.log', '1785-genkernel-3.0.1_beta8.log', 
'1750-kdetoys-3.1.4.log', '1748-kdegames-3.1.5.log', 
'1768-xfree-drm-4.3.0-r7.log', '1760-xpdf-2.02.1.log', 
'1785-kbd-1.08-r4.log', '1751-kdegraphics-3.1.4.log', 
'1778-lcms-1.12.log', '1798-psmisc-21.2-r4.log', 
'1790-python-2.3.3.log', '1809-curl-7.10.7.log', '1800-nmap-3.48.log', 
'1754-kde-3.1.5.log', '1749-kdetoys-3.1.5.log', 
'1793-libxslt-1.1.1.log', '1766-textutils-2.1.log', 
'1781-openmotif-2.1.30-r4.log', '1766-tcsh-6.12-r2.log', 
'1747-kdepim-3.1.5.log', '1749-kdegames-3.1.4.log', 
'1766-sh-utils-2.0.15.log', '1802-gaim-0.75-r8.log', 
'1797-perl-5.8.2-r1.log', '1799-psmisc-21.2-r3.log', 
'1789-tk-8.3.4-r1.log', '1779-lcms-1.11.log', 
'1807-ymessenger-1.0.4.1.log', '1787-fluxbox-0.9.7-r1.log', 
'1755-fluxbox-0.9.7-r1.log']


#Get the total size of the given directory

>>> os.path.getsize(portage_dir)
4872L

The next step is where I'm getting lost. I've tried many things but I'm 
lost. 

At this point I would like to have Python simply tell me the size of 
each individual file instead of the the size of the whole parent 
directory. Can someone, without given me the direct answer lead me in 
the right direction. Thanks

Joshua Banks
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.3 (GNU/Linux)

iD8DBQFAM4wyp9X7q/XgeyYRArC9AJ4gLfR+OLReehlw3GCiJZr9htWpLQCdFr9g
CVIYRQ9+kMFwoVks0zyp/zE=
=c5cC
-----END PGP SIGNATURE-----




More information about the Tutor mailing list