Python copy directory structure scripting code sharing _python

Source: Internet
Author: User
Tags anonymous mkdir python script

Introduction

There is a need to replicate the directory structure in a directory, do not file, when the directory structure is very small can be built manually, when the directory structure is complex, the level of the directory is very deep, a lot of time, this time if the manual to build, it is not a good way to get the dead. Write a Python script to handle it.

First understand

Learn a few things before writing a Python script

Copy Code code as follows:

#!/usr/bin/python

The person who wrote the script knows that the executor that is used to mark the script is similar to the
Copy Code code as follows:

#!/bin/bash is executed through bash.
#!/usr/local/php/bin/php through PHP Actuators

#-*-Coding:utf-8-*-


This is to set the encoding format of the script, otherwise the non-English may appear garbled

anonymous function lambda

Copy Code code as follows:

#lambda很好用, it is convenient to create anonymous functions
g = Lambda x,y:x+y
G (3,5) #返回8

Anonymous functions are divided into four parts, identifying lambda, semicolon:, Parameter x,y, Operation X+y

In addition to this, there are function map, filter one to map, one to filter

Copy Code code as follows:

__name__== "__main__"

A file is a module, in Python each module has a __name__ attribute, the value of the attribute depends on how to use the module, there are generally two ways to use, directly in the command line to run, this time __name__ value of __main__, when import use, __ The name__ value is the name of the current module (without the extension), so you can use this to determine whether you are running the program directly on the command line to do some scripting.
Copy Code code as follows:

Import OS
Import Sys

There are two modules, the OS contains some operating system functions, such as traversing folders, splicing paths, and so on, the SYS module contains system functions, I am here only to get the parameters behind the script

Coding

Copy Code code as follows:

#!/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):
 &NB sp;  paths = Os.listdir (src)
    paths = map (lambda name:os.path.join (src,name), paths)
    paths = Filter (Isdir, paths)
    if (len (Paths) <=0):
         return
    to 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 ("Cannot place the generated new directory in the source directory")
Else
If not Os.path.isdir (target):
Os.mkdir (target)
Mkfloders (Source,target)
Else
Print ("Source folder does not exist")

Use

The use is simple:

Copy Code code as follows:

#在当前文件夹下执行
./folders.py.//tmp/yyyyy

Create the YYYYY directory under #执行完之后就会在/tmp, which contains the directory structure in the first folder above

There are two places to watch this place, and you can't put the created directory in the directory you want to replicate or in its subdirectories

Summarize

I ran into this problem when I was doing this./usr/bin/python^m:bad interpreter:no such file or directory, this problem seems to be a coding problem, after each line added a character, after the data, The original is because I copied the program directly from Windows to Linux under the coding problem, the solution is very simple: VI folders.py, after the command line input

Copy Code code as follows:

: Set FF #结果表示编码平台, should be Fileformat=dos

: Set Fileformat=unix #设置编码到unix平台

: Set FF #这个时候再去查看文件编码, should be Fileformat=unix

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.