- when copy file with
shutil
andos.listdir()
IsADirectoryError: [Errno 21]
- The problem comes from the use of
os.listdir()
returns both files and folders which causes the confusion forshutil.copy()
function. - The resolution is the use of
os.walk()
function instead ofos.listdir()
- How to copy folder from source to deset in python.
- The why to copy folder form one place to another place is
shutil.copytree
. - The key usage of this function is to make sure the dest location is a completely new location.
- Demo code for copying one folder with many of the sub-folders into another location
import pandas as pd
import os
import shutil
file_path = '../DSB3/stage1'
target_path = '/media/loveplay1983/dl_lab/thesis/LungNoduleDetectionClassification/DSB3/malignancies'
csv_path = '../DSB3'
malig_tbl = pd.concat([pd.read_csv(csv_path+'/stage1_labels_1397.csv'), \
pd.read_csv(csv_path+'/stage1_solution_198.csv')])
malig_tbl.head()
patients = malig_tbl['id'].values
status = malig_tbl['cancer'].values
print(patients, status)
nods_file = os.listdir(file_path)
nods_index = []
for i, each in enumerate(status):
if each == 1:
nods_index.append(i)
nodes = malig_tbl.iloc[nods_index]
nodes_true = []
for each in nods_file:
for i, r in nodes.iterrows():
if each == r.id:
nodes_true.append(each)
print(len(nodes_true))
inx = 0
while inx != 419:
for each in nodes_true:
shutil.copytree(file_path+'/{}'.format(each), target_path+'/nods_{}'.format(inx))
inx += 1
- Python create and delete file demo
#!/usr/bin/python
#-*-coding:utf-8-*- #指定编码格式,python默认unicode编码
import os
directory = "./dir"
os.chdir(directory) #切换到directory目录
cwd = os.getcwd() #获取当前目录即dir目录下
print("------------------------current working directory------------------")
def deleteBySize(minSize):
"""删除小于minSize的文件(单位:K)"""
files = os.listdir(os.getcwd()) #列出目录下的文件
for file in files:
if os.path.getsize(file) < minSize * 1000:
os.remove(file) #删除文件
print(file + " deleted")
return
def deleteNullFile():
'''删除所有大小为0的文件'''
files = os.listdir(os.getcwd())
for file in files:
if os.path.getsize(file) == 0: #获取文件大小
os.remove(file)
print(file + " deleted.")
return
def create():
'''根据本地时间创建新文件,如果已存在则不创建'''
import time
t = time.strftime('%Y-%m-%d',time.localtime()) #将指定格式的当前时间以字符串输出
suffix = ".docx"
newfile= t+suffix
if not os.path.exists(newfile):
f = open(newfile,'w')
print newfile
f.close()
print newfile + " created."
else:
print newfile + " already existed."
return
hint = '''funtion:
1 create new file
2 delete null file
3 delete by size
please input number:'''
while True:
option = raw_input(hint) #获取IO输入的值
if cmp(option,'1') == 0:
create()
elif cmp(option,'2') == 0:
deleteNullFile()
elif cmp(option,'3') == 0:
minSize = raw_input("minSize(K):")
deleteBySize(minSize)
elif cmp(option,'q') == 0:
print "quit !"
break
else:
print ("disabled input ,please try again....")
- Lamda expression (转自https://www.cnblogs.com/hf8051/p/8085424.html)
- 应用在函数式编程中
Python提供了很多函数式编程的特性,如:map、reduce、filter、sorted等这些函数都支持函数作为参数,lambda函数就可以应用在函数式编程中。如下:
需求:将列表中的元素按照绝对值大小进行升序排列
list1 = [3,5,-4,-1,0,-2,-6]
sorted(list1, key=lambda x: abs(x))
当然,也可以如下:
list1 = [3,5,-4,-1,0,-2,-6]
def get_abs(x):
return abs(x)
sorted(list1,key=get_abs)
只不过这种方式的代码看起来不够Pythonic
- 应用在闭包中
def get_y(a,b):
return lambda x:ax+b
y1 = get_y(1,1)
y1(1) # 结果为2
当然,也可以用常规函数实现闭包
def get_y(a,b):
def func(x):
return ax+b
return func
y1 = get_y(1,1)
y1(1) # 结果为2
只不过这种方式显得有点啰嗦
那么是不是任何情况下lambda函数都要比常规函数更清晰明了呢?
肯定不是。
Python之禅中有这么一句话:Explicit is better than implicit(明了胜于晦涩),就是说那种方式更清晰就用哪一种方式,不要盲目的都使用lambda表达式。
网友评论