Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python script file directory traversal examples     - Oracle 11g RAC manually playing GI PSU patch ( (Database)

- Linux commands with browsing and downloading files (Linux)

- Linux system started to learn: the Linux syslog (Linux)

- MariaDB 10.0.X, the dynamic column support JSON format to obtain data (Database)

- Ubuntu in Vim editor display processing method Chinese garbled (Linux)

- Servlet life cycle code examples (Programming)

- How to install Go1.5 source development environment on Mac (Server)

- Django template inheritance child (Programming)

- Execute command sentence can result in equipment permanently bricked in Linux laptop (Linux)

- Linux LVM Logical Volume Management to resize partitions (Linux)

- Linux system - The understanding cpu load (Linux)

- top command causes the system load increases (Linux)

- Xshell configure SSH free password (Server)

- Linux process management related content (Linux)

- After restarting network services, DNS address failure (Linux)

- Using IntelliJ IDEA 13 integrated development environment to build Android (Linux)

- MongoDB query statistics grouping remove duplicate records (Database)

- 11.2.04 Oracle RAC directory crfclust.bdb file is too large, Bug 20186278 (Database)

- Linux environment RabbitMQ installation and monitoring of plug-in installation (Linux)

- Ubuntu 12.04 commonly use shortcuts finishing Share (Linux)

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

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
                    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)
            search (curname, filter, allfile)
    return allfile


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)
- Keepalived + Nginx Installation and Configuration (Server)
- Use Docker / LXC quickly launch a desktop system (Linux)
- Ubuntu program using the Sound Recorder (Linux)
- Strategy Games Ubuntu installation of Wesnoth 1.12.1 (Linux)
- 5 tips to improve your Linux desktop security (Linux)
- The first IOS Objective-C program (Programming)
- Linux hybrid character device (Linux)
- How to monitor Linux system performance Nmon (Linux)
- How to create a secure and easy to remember password (Linux)
- Ubuntu 14.04 next upgrade to GNOME 3.12 (Linux)
- Install Java, Maven, Tomcat under Linux (Linux)
- Linux iptables: basic principles and rules (Linux)
- Linux systems for entry-learning: Install closed-source packages in Debian (Linux)
- DELL D630 Wireless LAN Driver Installation CentOS6 (Linux)
- Common data structures and functions of Linux process scheduling (Programming)
- SaltStack installation and testing (Server)
- Du and df show disk space usage inconsistent Causes and Treatment (Linux)
- How to install Git client in Ubuntu (Linux)
- Broadcom transplanted to OpenWrt summary (Programming)
- Linux protobuf-c (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.