introductory
There is a need to copy the directory structure of a directory, do not want to file, when the directory structure is very small when you can manually create, when the directory structure is complex, the directory hierarchy is very deep, a lot of directories, this time if you still manually to create, it is not a good way, get it wrong will die. Write a python script to deal with it.
firstly, understand
A few things to know before writing python scripts
#!/usr/bin/python
This is known to anyone who has written a script, and is used to identify the executor of that script, similarly the
/bin/bash /bin/bash Execution via bash
/usr/local/php/bin/php /usr/local/php/bin/php via php executor
# -*- coding: utf-8 -*-
This is to set the encoding format of the script, otherwise non-English may appear messy code
Anonymous function lambda
#lambda works well and it's easy to create anonymous functions
g = lambda x,y : x+y
g(3,5) # Returns 8
The anonymous function is divided into four parts, the identifier lambda, the semicolon :, the arguments x,y, and the 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, and every module in python has a __name__ attribute, the value of which depends on how the module is used. There are two ways to use it: directly on the command line, where the __name__ value is __main__, and when import is used, where the __name__ value is the name of the current module (with no When import is used, the __name__ value is the name of the current module (without extension), so you can use this to determine whether to run the program directly from the command line in order to do some scripting.
import os
import sys
There are also these two modules, os contains some operating system functions, such as traversing folders, splicing paths, etc., and the sys module contains system functions, which I only use here to get the parameters behind the scripts
encodings
#!/usr/bin/python
# -*- coding: utf-8 -*-
#Filename:
import os
import sys
source = ([1])
target = ([2])
def isdir(x):
return (x) and x != '.svn'
def mkfloders(src,tar):
paths = (src)
paths = map(lambda name:(src,name),paths)
paths = filter(isdir, paths)
if(len(paths)<=0):
return
for i in paths:
(filepath, filename)=(i)
targetpath = (tar,filename)
not (targetpath) and (targetpath)
mkfloders(i,targetpath)
if __name__=="__main__":
if((source)):
if((source) == 0):
print("Could not place the generated new directory in the source directory.")
else:
if not (target):
(target)
mkfloders(source,target)
else:
print("Source folder does not exist")
utilization
It's easy to use:
# Execute in the current folder
./ ./ /tmp/yyyyy
# After execution the yyyyyy directory will be created under /tmp, containing the directory structure from the first folder above
There are two things to keep in mind in this area, you cannot place the created directory in the directory or subdirectory you want to copy.
summarize
When I was doing this, I encountered this problem /usr/bin/python^M: bad interpreter: No such file or directory, this problem looks like a coding problem, adding a character after each line, after checking the information, it turned out to be due to the fact that I copied the program directly from windows to linux. The solution is very simple: after vi, at the command line, type in
:set ff # The result indicates the encoding platform, which should be fileformat=dos
:set fileformat=unix #Set encoding to unix platforms
:set ff #This time check the file encoding again, it should be fileformat=unix