Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python script file directory traversal examples     - Nginx + uWSGI + Django + Python Application Architecture Deployment (Server)

- 20 open source / commercial Linux server management control panel (Server)

- Linux AS4 VPN server in conjunction with a firewall perfect (Linux)

- To generate a certificate using OpenSSL under Linux (Server)

- Mac OS X 10.9 build Nginx + MySQL + php-fpm environment (Server)

- RHEL6.5 replace local YUM source (Linux)

- 2016, the new Node project Precautions (Programming)

- Distributed transaction management Spring declarative transactions (Programming)

- You really do need to know a variety of programming languages (Programming)

- Go Languages Reviews (Programming)

- C ++ why we chose to use the smart pointer (Programming)

- Analyzing Linux server architecture is 32-bit / 64-bit (Server)

- How to install Laravel PHP framework on CentOS 7 / Ubuntu 15.04 (Server)

- Learn about EditText little depth (Programming)

- How to use Aptik to backup and restore Apps/PPAs under ubuntu (Linux)

- Oracle Execute to Parse perform analytical Ratio Analysis (Database)

- ActiveMQ-based shared file system HA solutions (Server)

- Adding SSH to Github (Linux)

- CentOS 6 compiling httpd-2.4.10 (Server)

- Kali Linux 2.0 U disk installation errors Your installation cd-rom could not be mounted (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:      
 
- BusyBox build root file system (Linux)
- 30 minutes with your Quick Start MySQL Tutorial (Database)
- grep command Series: grep command to search for multiple words (Linux)
- Linux use glibc2 library and crypt () function to generate the user password (Programming)
- MongoDB replication instance (Database)
- How to publish projects to the Jcenter repository using Gradle in Android Studio (Programming)
- Openfire achieve load balancing cluster by Nginx (Server)
- Distributed transaction management Spring declarative transactions (Programming)
- Ubuntu Linux Change the PATH (Linux)
- Use Linux built-in firewall to improve network access control (Linux)
- Depth understanding of the TCP protocol (Database)
- Compiling source code Nginx module installation subs_filter (Server)
- Intrusion prevention network server security maintenance tips (Linux)
- How to make a U disk to install Ubuntu (Linux)
- Schema snapshot rollback (Database)
- Python control multi-process and multi-threaded concurrency (Programming)
- Run Windows in FreeBSD bhyve (Linux)
- Hazelcast integration with MongoDB (Database)
- Java Collections Framework interfaces map (Programming)
- Netapp storage routine inspections and information gathering (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.