Python copy directory structure script code sharing

  • 2020-04-02 14:38:04
  • OfStack

The introduction

There is a need to copy a directory in the directory structure, do not file, when the directory structure is seldom can manually to establish, when the directory structure is complex, deep directory hierarchy, directory a lot of time, if this time or manual to set up, it is not a good method, lane is bad to be dead. Write a python script to handle it.

First of all know

Before you write a python script, learn a few things


#!/usr/bin/python

This is something that anyone who has ever written a script knows is used to indicate the executor of the script, and so on

#!/bin/bash       through bash To perform the
#!/usr/local/php/bin/php through php Executor to execute    
  # -*- coding: utf-8 -*-

This is to set the script encoding format, otherwise non-english may appear garbled code

Anonymous function lambda


#lambda It's nice, it's easy to create anonymous functions
g = lambda x,y : x+y
g(3,5) # return 8

The anonymous function is divided into four parts, identifying lambda, semicolon:, parameter x,y, operation x+y

In addition to this, there are functions map, filter, one for mapping, one for filtering


__name__=="__main__"

A file is a module, each module in python has a __name__ attribute, the value of the attribute depends on how to use this module, there are generally two kinds of usage, run directly on the command line, this time for __main__ __name__ values, when the import use __name__ value is the name of the current module (with no extension), so by the judge whether directly on the command line to run the program, in order to do some scripts to use.

import os
import sys

There are also two modules, OS contains some operating system functions, such as traversing folders, stitching paths, etc., and sys module contains system functions, which I will only use here to get the parameters after the script

coding


#!/usr/bin/python
# -*- coding: utf-8 -*-
#Filename:floders.py import os
import sys source = os.path.realpath(sys.argv[1])
target = os.path.realpath(sys.argv[2]) def isdir(x):
    return os.path.isdir(x) and x != '.svn'
def mkfloders(src,tar):
    paths = os.listdir(src)
    paths = map(lambda name:os.path.join(src,name),paths)
    paths = filter(isdir, paths)
    if(len(paths)<=0):
        return
    for i in paths:
        (filepath, filename)=os.path.split(i)
        targetpath = os.path.join(tar,filename)
        not os.path.isdir(targetpath) and os.mkdir(targetpath)
        mkfloders(i,targetpath) if __name__=="__main__":
    if(os.path.isdir(source)):
        if(target.find(source) == 0):
            print(" The generated new directory cannot be placed in the source directory ")
        else:
            if not os.path.isdir(target):
                os.mkdir(target)
            mkfloders(source,target)
    else:
        print(" The source folder does not exist ")

use

Easy to use:


# Execute in the current folder
./folders.py ./ /tmp/yyyyy # When you're done, you're going to be /tmp create yyyyy Directory that contains the directory structure from the first folder above

Two things to note about this place are that you cannot place the created directory in the directory you want to copy or in its subdirectories

conclusion

Met the problem when do the/usr/bin/python ^ M: bad interpreter: No to the file or directory, this problem it seems that is the problem of coding, behind each row added characters, check information, it is because I make a copy of the program directly from Windows to Linux under the coding problems, the solution is simple: vi folders. Py, under the command line input


:set ff # The result represents the coding platform, which should be fileformat=dos :set fileformat=unix # Set the encoding to unix platform :set ff # This time to check the file code, should be fileformat=unix


Related articles: