Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python script file directory traversal examples     - How to compare PDF files on Ubuntu (Linux)

- Python programming style (Programming)

- CentOS 5.11 Open VNC access (Linux)

- Ubuntu 12.04 installation OpenCV2.4.1 and compile test (Linux)

- JavaScript, some conclusions about the implicit conversion (Programming)

- Python Django model within the class meta Detailed (Programming)

- Log4Net (Linux)

- Linux process group, session daemon (Linux)

- VPS xen openvz kvm (Server)

- XtraBackup achieve non-stop use of master-slave synchronization service (Database)

- Download Manager uGet 2.0 installed in Debian, Ubuntu, Linux Mint and Fedora (Linux)

- Generate two-dimensional code parsing code Java (Programming)

- Loop arrays in C language is not easy to find a bug (Programming)

- How dependent on Fedora, CentOS, RHEL check RPM packages (Linux)

- Ubuntu users to install Gnome-Pie 0.6.5 (Linux)

- Linux network security strategy (Linux)

- MyCAT easy entry (Database)

- IBM Data Studio to use ---- window displays all rows (Database)

- Single list summarizes the basic operation (Programming)

- When a software RAID data recovery and reconstruction of how failure (Linux)

 
         
  Python script file directory traversal examples
     
  Add Date : 2018-11-21      
         
         
         
  Example

Himself wrote a Python script file traversal of found files for specific treatment. Lacks technical content, but also recorded it.

Code Copy the code below

#! / Usr / bin / python
# - * - Coding: utf-8 - * -
import sys
import os
import shutil
dir = "/ mnt / Packages"
class Packages:
    def __init __ (self, srcdir, desdir):
        self.sdir = srcdir
        self.ddir = desdir
    def check (self):
        print ( 'program start ...')
        for dirpath
 
, Dirnames, filenames in os.walk (self.sdir): www.1linuxidc.Net # Traversing File
            for filename in filenames:
                Absolute address thefile = os.path.join (dirpath, filename) # file
                try:
                    if os.path.splitext (thefile) [1] == '. rpm': # filter .rpm file format
                        #print ( 'Fount rpm package:' + thefile)
                        if 'inspuer' in os.popen ( 'rpm -qpi' + thefile) .read () rstrip ().:
                            print ( 'Found error package:' + thefile)
                            shutil.copy (thefile, self.ddir) # copy files to the wrong directory desdir
                            f = open ( 'list.txt', 'a') # error is written to the file list list.txt
                            f.write (filename + '')
                            f.close ()
                except IOError, err:
                    print err
                    sys.exit ()
  
if __name__ == '__main__':
    dir = Packages ( '/ mnt / cdrom', '/ mnt / erpm') # source directory is / mnt / cdrom, the target directory is / mnt / erpm
    dir.check ()

Examples, traversing the directory file

Code Copy the code below

def search (folder, filter, allfile):
    folders = os.listdir (folder)
    for name in folders:
        curname = os.path.join (folder, name)
        isfile = os.path.isfile (curname)
        if isfile:
            ext = os.path.splitext (curname) [1]
            count = filter.count (ext)
            if count> 0:
                cur = myfile ()
                cur.name = curname
                allfile.append (cur)
        else:
            search (curname, filter, allfile)
    return allfile

example

Traverse Folder and delete specific file format

Code Copy the code below

#! / Usr / bin / python
# - * - Coding: utf-8 - * -
 
import os
 
def del_files (path):
    for root, dirs, files in os.walk (path):
        for name in files:
            if name.endswith ( "tmp."):
                os.remove (os.path.join (root, name))
  print ( "Delete File:" + os.path.join (root, name))
 
# Test
if __name__ == "__main__":
    path = '/ tmp'
    del_files (path)
     
         
         
         
  More:      
 
- Java Database Programming JDBC configuration (Programming)
- CentOS network configuration 7, and set the host name and IP-bound problems (Linux)
- PXE + Kickstart automatically install CentOS 6.5 (Linux)
- Ubuntu resolve sudo: source: command not found error (Linux)
- How nodeclub constructed Docker image (Server)
- To use Linux to create a secure managed gateway (Linux)
- Ora-14450: attempt to access ah transactional temptable already in use (Database)
- How to install Laravel PHP framework on CentOS 7 / Ubuntu 15.04 (Server)
- System Security: Build Linux with LIDS steel castle (Linux)
- Elasticsearch 2.20 Highlight (Server)
- Nginx Performance Tuning Guidelines (Server)
- The first deployment of cross-platform operation Rafy record (Server)
- Linux tar compressed exclude a folder (Linux)
- Repair Chrome for Linux is (Linux)
- Solaris 11 forget the root password (Linux)
- Qt for file splitting and fusion gadgets (Programming)
- Killall five options you might not know (Linux)
- RHEL7 system making use of OpenStack mirror (Linux)
- CentOS 6.5 dual card configuration, one of the external network, a local area network connection (Linux)
- Laravel 4.2 Laravel5 comprehensive upgrade Raiders (Server)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.