mirror of
https://github.com/rasbt/python_reference.git
synced 2025-01-30 13:13:50 +00:00
copy find appr
This commit is contained in:
parent
f4817b90c2
commit
fe21bc4838
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:b8d5bf725bbe6b0e07d0b544cb5a65cdce7b4c162fd421985a4aa25ea57f9151"
|
"signature": "sha256:9e62469f6250ac6d58e6d3d2c67a5995d1505778b147ae918d71f4ebc1c12fb6"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -1270,14 +1270,14 @@
|
||||||
"import subprocess\n",
|
"import subprocess\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def subprocess_findcopy(path, search_str, dest): \n",
|
"def subprocess_findcopy(path, search_str, dest): \n",
|
||||||
" query = 'find %s -name \"%s\" -exec cp {}\" %s \\;' %(path, search_str, dest)\n",
|
" query = 'find %s -name \"%s\" -exec cp {} %s \\;' %(path, search_str, dest)\n",
|
||||||
" subprocess.call(query, shell=True)\n",
|
" subprocess.call(query, shell=True)\n",
|
||||||
" return "
|
" return "
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 2
|
"prompt_number": 30
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -1297,12 +1297,16 @@
|
||||||
"def walk_findcopy(path, search_str, dest):\n",
|
"def walk_findcopy(path, search_str, dest):\n",
|
||||||
" for path, subdirs, files in os.walk(path):\n",
|
" for path, subdirs, files in os.walk(path):\n",
|
||||||
" for name in fnmatch.filter(files, search_str):\n",
|
" for name in fnmatch.filter(files, search_str):\n",
|
||||||
" shutil.copy(os.path.join(path,name), dest)"
|
" try:\n",
|
||||||
|
" shutil.copy(os.path.join(path,name), dest)\n",
|
||||||
|
" except NameError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" return"
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 3
|
"prompt_number": 33
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
@ -1310,19 +1314,33 @@
|
||||||
"input": [
|
"input": [
|
||||||
"import timeit\n",
|
"import timeit\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def findcopy_timeit(inpath, outpath, search_str):\n",
|
||||||
|
" \n",
|
||||||
|
" shutil.rmtree(outpath)\n",
|
||||||
|
" os.mkdir(outpath)\n",
|
||||||
|
" print(50*'#')\n",
|
||||||
|
" print('subprocsess call')\n",
|
||||||
|
" %timeit subprocess_findcopy(inpath, search_str, outpath)\n",
|
||||||
|
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
|
||||||
|
" shutil.rmtree(outpath)\n",
|
||||||
|
" os.mkdir(outpath)\n",
|
||||||
|
" print('\\nos.walk approach')\n",
|
||||||
|
" %timeit walk_findcopy(inpath, search_str, outpath)\n",
|
||||||
|
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
|
||||||
|
" print(50*'#')\n",
|
||||||
|
"\n",
|
||||||
"print('small tree')\n",
|
"print('small tree')\n",
|
||||||
"inpath = '/Users/sebastian/Desktop/testdir_in'\n",
|
"inpath = '/Users/sebastian/Desktop/testdir_in'\n",
|
||||||
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
||||||
"searchstr = '*.png'\n",
|
"search_str = '*.png'\n",
|
||||||
"%timeit subprocess_findcopy(inpath, searchstr, outpath)\n",
|
"findcopy_timeit(inpath, outpath, search_str)\n",
|
||||||
"%timeit walk_findcopy(inpath, searchstr, outpath)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"print('\\nlarger tree')\n",
|
"print('larger tree')\n",
|
||||||
"inpath = '/Users/sebastian/Dropbox'\n",
|
"inpath = '/Users/sebastian/Dropbox'\n",
|
||||||
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
||||||
"searchstr = '*.csv'\n",
|
"search_str = '*.csv'\n",
|
||||||
"%timeit subprocess_findcopy(inpath, searchstr, outpath)\n",
|
"findcopy_timeit(inpath, outpath, search_str)\n"
|
||||||
"%timeit walk_findcopy(inpath, searchstr, outpath)"
|
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -1332,7 +1350,7 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"small tree\n",
|
"small tree\n",
|
||||||
"100 loops, best of 3: 8.48 ms per loop"
|
"##################################################"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1340,7 +1358,8 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"100 loops, best of 3: 22.3 ms per loop"
|
"subprocsess call\n",
|
||||||
|
"1 loops, best of 3: 268 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1348,9 +1367,23 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
|
"copied 13 files\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"os.walk approach\n",
|
||||||
|
"100 loops, best of 3: 12.2 ms per loop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"copied 13 files\n",
|
||||||
|
"##################################################\n",
|
||||||
"larger tree\n",
|
"larger tree\n",
|
||||||
"10 loops, best of 3: 7.13 ms per loop"
|
"##################################################\n",
|
||||||
|
"subprocsess call\n",
|
||||||
|
"1 loops, best of 3: 623 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1358,18 +1391,31 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"1 loops, best of 3: 413 ms per loop"
|
"copied 105 files\n",
|
||||||
|
"\n",
|
||||||
|
"os.walk approach\n",
|
||||||
|
"1 loops, best of 3: 417 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n"
|
"\n",
|
||||||
|
"copied 105 files\n",
|
||||||
|
"##################################################\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 4
|
"prompt_number": 35
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
{
|
{
|
||||||
"metadata": {
|
"metadata": {
|
||||||
"name": "",
|
"name": "",
|
||||||
"signature": "sha256:b8d5bf725bbe6b0e07d0b544cb5a65cdce7b4c162fd421985a4aa25ea57f9151"
|
"signature": "sha256:9e62469f6250ac6d58e6d3d2c67a5995d1505778b147ae918d71f4ebc1c12fb6"
|
||||||
},
|
},
|
||||||
"nbformat": 3,
|
"nbformat": 3,
|
||||||
"nbformat_minor": 0,
|
"nbformat_minor": 0,
|
||||||
|
@ -1270,14 +1270,14 @@
|
||||||
"import subprocess\n",
|
"import subprocess\n",
|
||||||
"\n",
|
"\n",
|
||||||
"def subprocess_findcopy(path, search_str, dest): \n",
|
"def subprocess_findcopy(path, search_str, dest): \n",
|
||||||
" query = 'find %s -name \"%s\" -exec cp {}\" %s \\;' %(path, search_str, dest)\n",
|
" query = 'find %s -name \"%s\" -exec cp {} %s \\;' %(path, search_str, dest)\n",
|
||||||
" subprocess.call(query, shell=True)\n",
|
" subprocess.call(query, shell=True)\n",
|
||||||
" return "
|
" return "
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 2
|
"prompt_number": 30
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
@ -1297,12 +1297,16 @@
|
||||||
"def walk_findcopy(path, search_str, dest):\n",
|
"def walk_findcopy(path, search_str, dest):\n",
|
||||||
" for path, subdirs, files in os.walk(path):\n",
|
" for path, subdirs, files in os.walk(path):\n",
|
||||||
" for name in fnmatch.filter(files, search_str):\n",
|
" for name in fnmatch.filter(files, search_str):\n",
|
||||||
" shutil.copy(os.path.join(path,name), dest)"
|
" try:\n",
|
||||||
|
" shutil.copy(os.path.join(path,name), dest)\n",
|
||||||
|
" except NameError:\n",
|
||||||
|
" pass\n",
|
||||||
|
" return"
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
"outputs": [],
|
"outputs": [],
|
||||||
"prompt_number": 3
|
"prompt_number": 33
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "code",
|
"cell_type": "code",
|
||||||
|
@ -1310,19 +1314,33 @@
|
||||||
"input": [
|
"input": [
|
||||||
"import timeit\n",
|
"import timeit\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"\n",
|
||||||
|
"def findcopy_timeit(inpath, outpath, search_str):\n",
|
||||||
|
" \n",
|
||||||
|
" shutil.rmtree(outpath)\n",
|
||||||
|
" os.mkdir(outpath)\n",
|
||||||
|
" print(50*'#')\n",
|
||||||
|
" print('subprocsess call')\n",
|
||||||
|
" %timeit subprocess_findcopy(inpath, search_str, outpath)\n",
|
||||||
|
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
|
||||||
|
" shutil.rmtree(outpath)\n",
|
||||||
|
" os.mkdir(outpath)\n",
|
||||||
|
" print('\\nos.walk approach')\n",
|
||||||
|
" %timeit walk_findcopy(inpath, search_str, outpath)\n",
|
||||||
|
" print(\"copied %s files\" %len(os.listdir(outpath)))\n",
|
||||||
|
" print(50*'#')\n",
|
||||||
|
"\n",
|
||||||
"print('small tree')\n",
|
"print('small tree')\n",
|
||||||
"inpath = '/Users/sebastian/Desktop/testdir_in'\n",
|
"inpath = '/Users/sebastian/Desktop/testdir_in'\n",
|
||||||
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
||||||
"searchstr = '*.png'\n",
|
"search_str = '*.png'\n",
|
||||||
"%timeit subprocess_findcopy(inpath, searchstr, outpath)\n",
|
"findcopy_timeit(inpath, outpath, search_str)\n",
|
||||||
"%timeit walk_findcopy(inpath, searchstr, outpath)\n",
|
|
||||||
"\n",
|
"\n",
|
||||||
"print('\\nlarger tree')\n",
|
"print('larger tree')\n",
|
||||||
"inpath = '/Users/sebastian/Dropbox'\n",
|
"inpath = '/Users/sebastian/Dropbox'\n",
|
||||||
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
"outpath = '/Users/sebastian/Desktop/testdir_out'\n",
|
||||||
"searchstr = '*.csv'\n",
|
"search_str = '*.csv'\n",
|
||||||
"%timeit subprocess_findcopy(inpath, searchstr, outpath)\n",
|
"findcopy_timeit(inpath, outpath, search_str)\n"
|
||||||
"%timeit walk_findcopy(inpath, searchstr, outpath)"
|
|
||||||
],
|
],
|
||||||
"language": "python",
|
"language": "python",
|
||||||
"metadata": {},
|
"metadata": {},
|
||||||
|
@ -1332,7 +1350,7 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"small tree\n",
|
"small tree\n",
|
||||||
"100 loops, best of 3: 8.48 ms per loop"
|
"##################################################"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1340,7 +1358,8 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"100 loops, best of 3: 22.3 ms per loop"
|
"subprocsess call\n",
|
||||||
|
"1 loops, best of 3: 268 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1348,9 +1367,23 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
|
"copied 13 files\n",
|
||||||
"\n",
|
"\n",
|
||||||
|
"os.walk approach\n",
|
||||||
|
"100 loops, best of 3: 12.2 ms per loop"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"output_type": "stream",
|
||||||
|
"stream": "stdout",
|
||||||
|
"text": [
|
||||||
|
"\n",
|
||||||
|
"copied 13 files\n",
|
||||||
|
"##################################################\n",
|
||||||
"larger tree\n",
|
"larger tree\n",
|
||||||
"10 loops, best of 3: 7.13 ms per loop"
|
"##################################################\n",
|
||||||
|
"subprocsess call\n",
|
||||||
|
"1 loops, best of 3: 623 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
|
@ -1358,18 +1391,31 @@
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n",
|
"\n",
|
||||||
"1 loops, best of 3: 413 ms per loop"
|
"copied 105 files\n",
|
||||||
|
"\n",
|
||||||
|
"os.walk approach\n",
|
||||||
|
"1 loops, best of 3: 417 ms per loop"
|
||||||
]
|
]
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"output_type": "stream",
|
"output_type": "stream",
|
||||||
"stream": "stdout",
|
"stream": "stdout",
|
||||||
"text": [
|
"text": [
|
||||||
"\n"
|
"\n",
|
||||||
|
"copied 105 files\n",
|
||||||
|
"##################################################\n"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
],
|
],
|
||||||
"prompt_number": 4
|
"prompt_number": 35
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"cell_type": "code",
|
||||||
|
"collapsed": false,
|
||||||
|
"input": [],
|
||||||
|
"language": "python",
|
||||||
|
"metadata": {},
|
||||||
|
"outputs": []
|
||||||
},
|
},
|
||||||
{
|
{
|
||||||
"cell_type": "markdown",
|
"cell_type": "markdown",
|
||||||
|
|
Loading…
Reference in New Issue
Block a user