我在学习 Python 并试图让它做一些简单的事情时遇到了令人沮丧的时间。 我有一个我想模拟的批处理文件,没什么特别的,它只是将文件从一个目录移动到另一个简单的东西。
xcopy /s K:\PSADMISS\Scans4Slideroom\VikramTestArea G:\A-Admissions\SlideRoom\VikramTestArea\AP_IB
正如您所见,它非常简单,只需将 K: 驱动器上的此文件夹中的文件移动到 G: 驱动器上的文件夹即可。然而,事实证明,尝试用 Python 重写它是一项令人沮丧的挑战。
到目前为止,这是我在 python 中所拥有的:
from shutil import copy2
copy2(r"K:\PSADMISS\Scans4Slideroom\VikramTestArea", "G:\A-Admissions\SlideRoom\VikramTestArea\AP_IB")
我什至第二次尝试过,但无济于事,而且使用 Python 非常混乱:
#import os
#import shutil
#import copyfile
##os.chdir('C:\\') #Make sure you add your source and destination path below
#src = ("\\aicfs01\A-Admissions\SlideRoom\VikramTestArea\\")
#dst = ("\\aicps01\PSADMISS\Scans4Slideroom\VikramTestArea\\")
#for filename in os.listdir(dir_src):
# if filename.endswith('.pdf'):
# shutil.copy( dir_src + filename, dir_dst)
# print(filename)
from shutil import copyfile
copyfile(r"K:\PSADMISS\Scans4Slideroom\VikramTestArea", "Admissions\SlideRoom\VikramTestArea\AP_IB")
#"G:\A-Admissions\SlideRoom\VikramTestArea\AP_IB"
无论哪种方式,我都在 python 中遇到相同的错误:
<块引用>回溯(最近一次调用最后一次): 文件“C:\Users\vmedhe\AppData\Local\Programs\Python\Python38-32\Scripts\SlideRoomKtoG.py”,第 15 行,在 copyfile(r"K:\PSADMISS\Scans4Slideroom\VikramTestArea", "Admissions\SlideRoom\VikramTestArea\AP_IB") 文件“C:\Users\vmedhe\AppData\Local\Programs\Python\Python38-32\lib\shutil.py”,第 259 行,在复制文件中 使用 open(src, 'rb') 作为 fsrc, open(dst, 'wb') 作为 fdst: PermissionError: [Errno 13] 权限被拒绝: 'K:\PSADMISS\Scans4Slideroom\VikramTestArea'
我一直收到这个权限错误,尽管我知道这是因为批处理文件有效。我怎样才能让python至少模拟这个批处理文件?它只有一行代码,但让 Python 如此令人沮丧似乎真的很烦人。