Get files under directory by python (transfer)

Gets the file name and the number of files in the specified directory, and then lists the directory names that still exist:

import os,sys
def listdir(dir,file):
  file.write(dir +'\n')
  fielnum =0
  list = os.listdir(dir)#List all files and directories in the directory
  for line in list:
    filepath = os.path.join(dir,line)
    if os.path.isdir(filepath):#If filepath is a directory, all files in the directory are listed again
      myfile.write(' '+ line +'//'+'\n')
      for li in os.listdir(filepath):
        myfile.write(' '+li +'\n')
        fielnum = fielnum +1
    elif os.path:#If filepath is a file, list the file name directly
      myfile.write(' '+line +'\n')
      fielnum = fielnum +1
myfile.write('all the file num is '+ str(fielnum))
dir = raw_input('please input the path:')
myfile = open('list.txt','w')
listdir(dir,myfile)
myfile.close()

The obtained file name and directory information will generate a list.txt file under the current path. The contents of the document are as follows:

G:\codes\python\file_info
list.txt
list2.txt
list_filenames.py
list_filenames2.py
list_files+.py
test//
g.txt
mmm.pptx
test2//
list.txt
test3
all the file num is9

Here is the upgraded version of the above program. After traversing the folders under the current directory, continue to execute the command of traversing the filename.

"""os.walk(path),ergodic path,Returns an object,
//Every part of him is a triple,
('Catalog x',[Catalog x Directory under list],Catalog x The following file)"""

import os
def walk_dir(dir,fileinfo,topdown=True):
for root, dirs, files in os.walk(dir, topdown):
for name in files:
print(os.path.join(name))
fileinfo.write(os.path.join(root,name)+'\n')
for name in dirs:
print(os.path.join(name))
fileinfo.write(' '+ os.path.join(root,name)+'\n')
dir = raw_input('please input the path:')
dir = r'G:\codes\python\file_info'
fileinfo = open('list2.txt','w')
walk_dir(dir,fileinfo)
fileinfo.close()

The result of shell printing is:

please input the path:G:\codes\python\file_info
list.txt
list2.txt
list_filenames.py
list_filenames2.py
list_files+.py
test
test2
g.txt
mmm.pptx
list.txt
test3
234.txt

The contents of list2.txt in the execution file directory are:

G:\codes\python\file_info\list.txt
G:\codes\python\file_info\list2.txt
G:\codes\python\file_info\list_filenames.py
G:\codes\python\file_info\list_filenames2.py
G:\codes\python\file_info\list_files+.py
G:\codes\python\file_info\test
G:\codes\python\file_info\test2
G:\codes\python\file_info\test\g.txt
G:\codes\python\file_info\test\mmm.pptx
G:\codes\python\file_info\test2\list.txt
G:\codes\python\file_info\test2\test3
G:\codes\python\file_info\test2\test3\234.txt

There is a certain space at the beginning of the line in the directory.

The following is another enhanced version, which mainly implements traversal of all files in a given directory, and calculates the MD5 value of all files, and then saves the MD5 value and file name in the list3.txt file in the directory. The code is as follows:

import os
import sys
import md5

def walk_dir(dir,fileinfo,topdown=True):
    for root, dirs, files in os.walk(dir, topdown):
        for name in files:
            path = os.path.join(root,name)
            md5v = sumfile(path)
            newpath = path.replace(dir,'')
            fileinfo.write(newpath + ':' + md5v + '\n')

def sumfile(fpath):
    m = md5.new()
    fobj = open(fpath)
    while True:
        d = fobj.read(8096)
        if not d:
            break
        m.update(d)
    return m.hexdigest()
#Get the current path of the script file
def cur_file_dir():
    #Get script path
    path = sys.path[0]
    #Determine whether it is a script file or a py2ex compiled file. If it is a script file, it returns the directory of the script. If it is a py2ex compiled file, it returns the compiled file path
    if os.path.isdir(path):
        return path
    elif os.path.isfile(path):
        return os.path.dirname(path)
#Print results
print cur_file_dir()

def main():
    #dir = raw_input('please input the path:')
    dir = cur_file_dir()
    fileinfo = open('list3.txt','w')
    walk_dir(dir,fileinfo)

if __name__ == '__main__':
    main()

After running, the contents of list3.txt file are as follows:

\list.txt:0a732356981d24beab0d0c1c4092b2a7
\list2.txt:6cebc24b3a996be27c732557a2ce545f
\list3.txt:d41d8cd98f00b204e9800998ecf8427e
\list_filenames.py:9201c1eefcaf2fd04478cc2b6430c678
\list_filenames2.py:ca3f74bdb0a5485dd5d95d77da80141b
\list_files+.py:720d569d2f07f0c80e0d695db31749ab
\test\g.txt:d41d8cd98f00b204e9800998ecf8427e
\test\mmm.pptx:6f0e81ad84c22838337f0f619080e7f2
\test2\list.txt:d41d8cd98f00b204e9800998ecf8427e
\test2\test3\234.txt:d41d8cd98f00b204e9800998ecf8427e

Original address: http://vipscu.blog.163.com/blog/static/181808372201221121244906/

Tags: Python shell

Posted on Tue, 05 May 2020 05:04:07 -0400 by gowni