我只是尝试复制具有唯一组名的文件,第二个_。总共有三个这样的组。我在尝试着将它们分成3个不同的文件夹。我一直有问题与shutil.copy。 Python版本3。
import os, os.path, shutil
folder_path = 'nodelists'
all_cells = [f for f in os.listdir(folder_path) if
os.path.isfile(os.path.join(folder_path, f))]
print("Length of all cells:",len(all_cells))
count=0
for cell in all_cells:
folder_name = cell.split('_')[2]
new_path = folder_name+"_nodelists"
if not os.path.exists(new_path):
os.mkdir(new_path)
old_cell_path = os.path.join(folder_path, cell)
shutil.copy(old_cell_path,new_path)
count+=1
if count % 500 == 0:
print ("Progress:",count, " done")
print("cell id is", str(cell))
### OUTPUT:Length of all cells: 15533
Progress: 500 done
cell id is AAGCGTTCAACTAGAA-1_nodelist_old_.csv
Progress: 1000 done
...
DISPLAYS RESULTS CORRECTLY! I am even able to open and check these files.##
...
Progress: 9500 done
cell id is GGGACCTAGCGACTAG-1_nodelist_postnatal_.csv
---------------------------------------------------------------------------
PermissionError Traceback (most recent call last)
<ipython-input-2-90d1a4cf5db0> in <module>()
20 #new_cell_path = os.path.join(new_path, cell)
21 #shutil.copy(old_cell_path, new_cell_path)
---> 22 shutil.copy(old_cell_path,new_path)
23
24 count+=1
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copy(src, dst, follow_symlinks)
239 if os.path.isdir(dst):
240 dst = os.path.join(dst, os.path.basename(src))
--> 241 copyfile(src, dst, follow_symlinks=follow_symlinks)
242 copymode(src, dst, follow_symlinks=follow_symlinks)
243 return dst
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
120 with open(src, 'rb') as fsrc:
121 with open(dst, 'wb') as fdst:
--> 122 copyfileobj(fsrc, fdst)
123 return dst
124
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copyfileobj(fsrc, fdst, length)
77 """copy data from file-like object fsrc to file-like object fdst"""
78 while 1:
---> 79 buf = fsrc.read(length)
80 if not buf:
81 break
PermissionError: [Errno 13] Permission denied
#我尝试通过“以管理员身份运行”来授予Jupyter权限。这些似乎仍然没有通过。我正在使用Windows 10并且无法查找我的文件夹的安全标签。所有这些文件夹都在我的盒子上同步文件夹。
非常感谢您的帮助!
编辑:我尝试过:尝试/除PermissionError外,我现在得到:
---------------------------------------------------------------------------
OSError Traceback (most recent call last)
<ipython-input-8-909c0ceea90a> in <module>()
19
20 try:
---> 21 shutil.copy(old_cell_path,new_path)
22
23 except PermissionError as error:
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copy(src, dst, follow_symlinks)
239 if os.path.isdir(dst):
240 dst = os.path.join(dst, os.path.basename(src))
--> 241 copyfile(src, dst, follow_symlinks=follow_symlinks)
242 copymode(src, dst, follow_symlinks=follow_symlinks)
243 return dst
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copyfile(src, dst, follow_symlinks)
120 with open(src, 'rb') as fsrc:
121 with open(dst, 'wb') as fdst:
--> 122 copyfileobj(fsrc, fdst)
123 return dst
124
~\AppData\Local\Continuum\anaconda3\lib\shutil.py in copyfileobj(fsrc, fdst, length)
77 """copy data from file-like object fsrc to file-like object fdst"""
78 while 1:
---> 79 buf = fsrc.read(length)
80 if not buf:
81 break
OSError: [Errno 22] Invalid argument
任何人都可以帮助调试吗?谢谢!
看来成功完成了9500多个条目。我敢打赌,其中一个文件夹或文件之一都存在一些权限问题。打开powershell,导航到目录的目录,然后运行类似的内容:
gci | ForEach-Object -Process {Get-Acl -Path $_.FullName}
在提供的示例中,我们正在查看目录的顶层,并获取目录中每个项目的访问控制级别。将-Recurse
参数添加到gci
也会遍历所有子目录和子项,并且可能正是您要的内容。一旦powershell创建了列表,您就可以过滤和浏览直到找到问题文件。
或者,您可以将python复制功能包装在try / catch中,并在python中记录错误的文件/目录名称,然后返回错误文件的列表,然后按事实返回以手动编辑这些项目的权限。] >
尝试后,我能够进行复制而没有任何错误:尝试/除了PermissionError和OSError。即使出现了这些异常,它也以某种方式从未经历过“例外”,而是成功复制了所有文件。我不确定原因和方式,但这成功完成了。感谢您的所有帮助!