Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Python script file directory traversal examples     - Nagios (centreon) monitoring Linux Log (Server)

- count (*) function in MySQL optimization of InnoDB storage engine (Database)

- Ubuntu install Eclipse can not find JAVA_HOME problem (Linux)

- Linux MultiPath software implementation instructions (Linux)

- C ++ free store and heap (Programming)

- It is time to upgrade your gulp 4.0 (Programming)

- Linux port mapping system (routing and forwarding) (Server)

- MySQL how to export files with the date format (Database)

- Setting up Linux machine through a proxy firewall (Linux)

- Install Git on CentOS (Linux)

- grep command Series: How to Use the grep command in Linux / UNIX (Linux)

- Linux console password solution (Programming)

- How to install with JSON support in Ubuntu 15.04 SQLite 3.9.1 (Database)

- CentOS modify yum update source (Linux)

- The Linux C truncate function clears the file notes (Programming)

- Ubuntu Slingscold (Linux)

- Install Jetty on CentOS / RHEL 6.X (Server)

- Linux Network Programming - raw socket instance: MAC header message analysis (Programming)

- Under Linux using Magent + Memcached cache server cluster deployment (Server)

- Use MongoDB C # MongoDB official driving operation (Database)

  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)
- MongoDB 3.2 to upgrade from 3.0.7 (Database)
- CentOS 6.4 of cron scheduled task configuration (Linux)
- MySQL / MariaDB database master-slave replication (Database)
- JavaScript closures and the scope chain (Programming)
- Let Markdown code syntax highlighting and support Django1.6 (Linux)
- httpd-2.4 feature (Server)
- Nginx introduced Dynamic Module Architecture (Server)
- Installation Sublime Text 3 (Build 3065) text editor in Ubuntu (Linux)
- Linux / Unix desktops interesting: Christmas tree on the terminal (Linux)
- Oracle11g CRS-0184 Problem Solving (Database)
- Computer security protection remove local and remote system log files (Linux)
- Oracle rebuild index script (Database)
- Apache Spark1.1.0 deployment and development environment to build (Server)
- PLSQL Developer synchronization table tools (Database)
- In Debian 4.3 compiler under Linux-2.6.28 kernel Summary (Programming)
- Ubuntu Tutorial: How to Upgrade a New Linux Kernel 3.12.7 on Ubuntu (Linux)
- Linux account management add relevant directives (Linux)
- Ubuntu under Spark development environment to build (Server)
- Oracle PLS-00231 error analysis (Database)
- Linux Defensive / mitigate DDOS attacks (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.