Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Python script file directory traversal examples     - Strategy Games Ubuntu installation of Wesnoth 1.12.1 (Linux)

- RHEL7 unattended automatic installation DHCP + TFTP + SYSLINUX + TFTP + Kickstart (Linux)

- GAMIT learning materials finishing (Linux)

- Iptables in Ubuntu (Linux)

- Redhat Close SELinux correct step (Linux)

- Ubuntu install Liferea news subscription software (Linux)

- Linux common commands MEMO (Linux)

- Linux kernel update error, update-initramfs: failed Solution (Linux)

- ssh port forwarding Comments (Server)

- linux smartd [FAILED] appears at startup (Linux)

- Ubuntu 15.04 / 14.04 install Ubuntu After Install 2.6 (Linux)

- ActiveMQ5.11.1 and JDK version matching relation (Linux)

- ntop monitoring software configuration and installation (Linux)

- Experience CoreCLR stack unwinding characteristics of initial implementation on Linux / Mac (Linux)

- Java 8 stream parsed into SQL (Programming)

- Weld a problem in JDK 8u60 environment (Programming)

- Oracle in the add & split partition on the impact of global & local index (Database)

- The difference between statement and preparedStatement of the jdbc (Database)

- Git Installation and Configuration (Network Agent settings) (Linux)

- Use libpq under Ubuntu 14.04 (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:      
 
- Debian 7.8 system installation and configuration process (Linux)
- Fedora && Arch Linux - the most romantic thing to happen now (Linux)
- How to install CentOS CentOS web page (Server)
- Enterprise Encrypting File System eCryptfs Comments (Linux)
- MySQL server after an unexpected power outage can not start (Database)
- Standard and IO redirection (Linux)
- Zabbix API and PHP configuration (Server)
- Teach you how to protect the security of Linux desktop (Linux)
- Linux environment installation of rvm and ruby (Linux)
- SELinux multi-level security (Linux)
- HTML5 Fundamentals study notes (Programming)
- Linux systems use IP masquerading anti-hacker (Linux)
- Android studio multi-channel ultra-compact version of the package (Programming)
- Port Telnet command to detect the remote host is turned on (Linux)
- To setup CentOS LAMP environment (Server)
- Shell scripts quickly deploy Tomcat project (Server)
- Java string intern constant pool resolution Introduction (Programming)
- wget command usage Explanation (Linux)
- Hive handle count distinct inclination to produce data processing (Database)
- Python when automated operation and maintenance often used method (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.