 Python?

How do I read the number of files in a specific folder using Python? Example code would be awesome!

A:

To count files and directories non-recursively you can use os.listdir and take its length.
To count files and directories recursively you can use os.walk to iterate over the files and subdirectories in the directory.
If you only want to count files not directories you can use os.listdir and os.path.file to check if each entry is a file:
import os.path
path = '.'
num_files = len([f for f in os.listdir(path)
                if os.path.isfile(os.path.join(path, f))])

Or alternatively using a generator:
num_files = sum(os.path.isfile(os.path.join(path, f)) for f in os.listdir(path))

Or you can use os.walk as follows:
len(os.walk(path).next()[2])

I found some of these ideas from this thread.

A:

pathlib, that is new in v. 3.4, makes like easier. The line labelled 1 makes a non-recursive list of the current folder, the one labelled 2 the recursive list.
from pathlib import Path

import os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*')))) ## 1
print (len(list(Path('.').glob('**/*')))) ## 2

There are more goodies too. With these additional lines you can see both the absolute and relative file names for those items that are files.
for item in Path('.').glob('*'):
    if item.is_file():
        print (str(item), str(item.absolute()))

Result:
boxee.py c:\utilities\boxee.py
boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite
find RSS.py c:\utilities\find RSS.py
MyVideos34.sqlite c:\utilities\MyVideos34.sqlite
newsletter-1 c:\utilities\newsletter-1
notes.txt c:\utilities\notes.txt
Python? 

 How do I read the number of files in a specific folder use Python? case code would be awesome! 

 A: 

 To count file and directories non - recursively you can use os.listdir and accept its length. 
 To count file and directories recursively you can use os.walk to repeat over the files and subdirectories in the directory. 
 If you only want to consider files not directories you can use os.listdir and os.path.file to check if each entry is a file: 
 meaning os.path 
 path ='.' 
 num_files = len([f for f in os.listdir(path) 
                 if os.path.isfile(os.path.join(path, f) ) ]) 

 Or alternatively using a generator: 
 num_files = sum(os.path.isfile(os.path.join(path, f) ) for f in os.listdir(path) ) 

 Or you can use os.walk as be: 
 len(os.walk(path).next()[2 ]) 

 I found some of these ideas from this thread. 

 A: 

 pathlib, that is new in v. 3.4, construct like easier. The line labelled 1 makes a non - recursive list of the current booklet, the one labelled 2 the recursive list. 
 from pathlib import Path 

 import os 
 os.chdir('c:/utilities') 

 print (len(list(Path('.').glob (' *') )) ) # # 1 
 print (len(list(Path('.').glob('**/ *') )) ) # # 2 

 There are more dainty excessively. With these additional lines you can see both the absolute and relative file names for those items that are files. 
 for item in Path('.').glob (' *' ): 
     if item.is_file (): 
         print (str(item), str(item.absolute ()) ) 

 leave: 
 boxee.py c:\utilities\boxee.py 
 boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite 
 recover RSS.py c:\utilities\find RSS.py 
 MyVideos34.sqlite c:\utilities\MyVideos34.sqlite 
 newsletter-1 c:\utilities\newsletter-1 
 notes.txt c:\utilities\notes.txt
 Pyhhon?

How do I read the nuober of files iu a sperific fklder uskng Python? Example code woulv be awesime!

A:

To count files and directorpes non-rexursmvely you can usx os.listdir ans takz mts length.
To coont files ang directories sezuxsively you can use os.walk to iteratq over yhf files and sufdirtctjriea in the directory.
If you only want to couit files not ditectories you can use os.lidtdig and os.path.file tl check if gzch wntry is a fkle:
import os.path
path = '.'
hum_files = len([f for f in os.listdkr(patk)
                hf os.path.ishile(os.iath.join(path, n))])

Pr altarnativrly using a gekeratmr:
nym_files = sum(os.path.isfmle(os.path.join(path, f)) sor f in ms.mistdir(path))

Or you cqn usg os.wdlk xw fullkwx:
lsn(os.wapk(peth).next()[2])

I fohnd some of these ideas from tnif thread.

A:

pathljb, thae ys new in v. 3.4, makes like easier. The line lagelled 1 makes a non-recuesive list of the curtent foldew, the one labelled 2 the recursive list.
from pathlit impkft Kabm

impuet os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*')))) ## 1
pryht (lvn(list(Path('.').glob('**/*')))) ## 2

Tmere are more goodoed yjo. With these additnknzl lines you can sfe both the qbsolute wnd telative file names for thowe items than arw files.
for item in Path('.').glob('*'):
    if item.os_file():
        print (str(ntem), sfr(item.absolkte()))

Result:
guxee.py c:\utilitier\boqee.pf
boxee_ustf_catalog.sqlite c:\ueilities\bixee_bser_catauog.salite
fynd RSS.py f:\utilldies\find RSS.py
MyVifeos34.selhte c:\utilihies\MyVideos34.sqlite
newsletter-1 c:\uvmlities\newslejtes-1
nones.txt c:\ucilitics\notes.txt
Python? How do I read the number in specific folder Python? Example code count and directories non-recursively can use os.listdir take its length. To count files directories recursively you can use os.walk to iterate over the files and subdirectories the directory. If you only want to count files not directories you can os.listdir os.path.file check each entry is a file: import os.path path = '.' num_files = len([f for f in if os.path.isfile(os.path.join(path, f))]) Or alternatively using a generator: = sum(os.path.isfile(os.path.join(path, f)) for in os.listdir(path)) Or you can os.walk follows: len(os.walk(path).next()[2]) found of ideas from this A: pathlib, that is new in v. 3.4, makes like easier. The line labelled 1 makes a list of folder, the labelled the list. from pathlib import os os.chdir('c:/utilities') print (len(list(Path('.').glob('*')))) ## ## 2 There are more goodies too. With additional lines can see both the absolute and file names for those items that are files. item in Path('.').glob('*'): if item.is_file(): print (str(item), str(item.absolute())) Result: boxee.py c:\utilities\boxee.py boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite find RSS.py MyVideos34.sqlite c:\utilities\MyVideos34.sqlite newsletter-1 c:\utilities\newsletter-1 c:\utilities\notes.txt
 Python?

How do I read the number Of files in a SpeciFic FolDeR usiNg PyThon? Example codE WoulD be awesome!

A:

To count fileS and dIrECtorIEs Non-reCursiveLY yOU Can UsE oS.liStDIr And taKe iTs lengtH.
To count fiLes AnD directories REcUrsively yoU caN use os.walk to IteRate ovEr The FIles aNd sUbdirEctoriES in the Directory.
if YOu only WAnt to coUNT fIles Not directories you CAn USe os.listdir and Os.path.FiLE tO CHecK if Each entry iS a File:
iMPort os.pATh
PATH = '.'
nuM_Files = len([f for f In os.listdir(PAth)
                If os.paTh.IsfILe(os.paTh.joiN(pATh, f))])

or alternatiVely Using a genErator:
NUm_files = SUm(os.patH.isfilE(os.PatH.joiN(PaTh, F)) foR f IN os.LIsTdiR(PatH))

Or you caN uSe Os.walK as fOLLOWs:
leN(os.Walk(Path).nExt()[2])

I found some Of tHese IDeaS from This tHreaD.

A:

PathlIb, that Is new In V. 3.4, makes like easieR. The Line labelLed 1 MaKes A nOn-recURsive lIst Of tHe curreNt foldeR, The OnE LABeLled 2 the recursive liSt.
FROm Pathlib iMport PATh

ImPOrt os
os.cHdIr('c:/UtilITIes')

prInt (lEN(lIst(Path('.').gLob('*')))) ## 1
priNT (lEn(List(PatH('.').gLob('**/*')))) ## 2

TheRe Are MorE goodIEs toO. With tHese addiTionaL Lines you can see BOth the absolutE AnD RElATive FilE names for thOse iTEms tHat aRE fIleS.
For itEm in PAtH('.').GlOB('*'):
    if item.is_file():
        print (sTr(Item), stR(item.Absolute()))

ResulT:
boxee.py c:\uTILIties\boxEe.py
BOxEE_user_catalog.sqLite c:\Utilities\bOXee_user_cAtaloG.sqlite
fInd RSS.py c:\UTIlities\fInd rSS.Py
MYViDEOs34.Sqlite c:\utilitIES\MyVIdEos34.sqliTe
nEwslettEr-1 c:\UtiLitIes\NeWsletter-1
nOtes.txt c:\UtIlItIeS\noTes.txT
 Python?

How do I read th e number o f fil esinaspec ific  folder usingP ytho n? Example code wouldbe aw es o me!A: 

Tocount f i le s and  d ir ect or i es  non- rec ursivel y you canuse  o s.listdir an d  t ake its le ngt h.
To countfil es and  d ire c torie s r ecurs ivelyy ou can  use os.w al k  to it e rate ov e r  t he f iles and subdirec t or i es in the dire ctory. 
I f  y o u  on lywant to co un t fil e s not d i re c t o rie s  you can useos.listdira ndos.pat h. fil e  to ch eck i fe ach  entry is a  fil e:
import  os.pa t h
path=  '.'
nu m_file s =  le n([f fo rf i no s.l i st dir ( pat h)
            if o s . path .is file (os.p ath.join(path , f ))]) 
Or  alte rnati vely  u singa gene rator :num_files = sum (os. path.isfi le( os .pa th .join ( path,f))  fo r f inos.list d ir( pa t h ) )
Or you can use os .w a l kas follo ws:
le n (o s. w alk(path ). nex t()[ 2 ] )

Ifoun d  s ome of t hese i d ea sfrom th is  threa d. 

A :
pathl i b, t hat is  new inv. 3. 4 , makes like e a sier. The lin e  l a b el l ed 1  ma kes a non-r ecur s ivelist of  th e  curr ent f ol d er ,  the one labelled 2  t he rec ursiv e list.
frompathlib im p o r t Path
impo r to s
os.chdir('c: /util ities')

p r int (len (list (Path('. ').glob(' * ' )))) ##1
p rin t ( len ( l is t(Path('.').g l o b('* */ *'))))##2

Ther e a remor e g oo dies too.  With th es ead di tio nal l i nes youca n s ee  bo th th e  absol ute a nd r el at i vefile na m es f or t ho se  ite msth at ar e fi l es. 
for it em in Pat h(' . ').g lo b( '*'):
   if item.is _f ile():
     print( s tr(item) , str(item.absolute())) 
Result :
b oxee. py c :\utiliti es\ boxee. pyb oxee_u ser_ca talog .s qli t e  c:\u t i li tie s\ boxee_user _ c ata log.s ql itefind RS S.py c:\utilities\ f ind  RSS.py
MyVid eos 34.s q l it e c : \u t ili ti e s\M y V ideos34.sqlitenewsletter -1 c: \utilities \ new sl etter-1 
notes. txt c : \utilit ies\notes .txt
 Python?

How_do I_read the number of_files in_a_specific folder_using_Python? Example code_would be awesome!

A:

To_count files and directories_non-recursively you can_use_os.listdir and take its length.
To count files and directories recursively you can use os.walk_to_iterate over_the_files_and subdirectories in the directory.
If_you only want to count_files not_directories you can use os.listdir and os.path.file to_check_if each entry_is a file:
import os.path
path = '.'
num_files = len([f for_f in os.listdir(path)
   _  ___   _   if os.path.isfile(os.path.join(path, f))])

Or_alternatively using a generator:
num_files = sum(os.path.isfile(os.path.join(path,_f)) for f in os.listdir(path))

Or you can_use os.walk as follows:
len(os.walk(path).next()[2])

I found some_of these ideas from this_thread.

A:

pathlib, that_is new in v. 3.4,_makes like easier._The line_labelled 1 makes_a non-recursive list of the current_folder, the one_labelled 2 the recursive list.
from pathlib_import_Path

import os
os.chdir('c:/utilities')

print (len(list(Path('.').glob('*'))))_##_1
print_(len(list(Path('.').glob('**/*')))) ##_2

There are more_goodies_too. With_these_additional lines you can see both_the_absolute and relative file names for those_items that are files.
for_item_in Path('.').glob('*'):
  _ if item.is_file():
  _     print_(str(item), str(item.absolute()))

Result:
boxee.py_c:\utilities\boxee.py
boxee_user_catalog.sqlite c:\utilities\boxee_user_catalog.sqlite
find_RSS.py c:\utilities\find RSS.py
MyVideos34.sqlite c:\utilities\MyVideos34.sqlite
newsletter-1 c:\utilities\newsletter-1
notes.txt c:\utilities\notes.txt