0
ファイル名の最初の3文字に基づいて、フォルダを作成してから関連ファイルにコピーします。ファイル名に基づいてマルチプルファイルをコピーする
私は最初に動作するスクリプトを持っていますが、複数回実行するとエラーが発生します ファイルが最初に存在するかどうかを確認する必要があると思いますが、 または任意の助けをいただければ幸いですos.list
から新しく作成したフォルダをフィルタする:
srcpath = 'C:\\temp\\Test'
srcfiles = os.listdir(srcpath)
destpath = 'C:\\temp\\Test'
# extract the three letters from filenames
destdirs = list(set([filename[0:3] for filename in srcfiles]))
def create(destdirs, destpath):
full_path = os.path.join(destpath, destdirs)
if not os.path.exists(full_path):
os.mkdir(full_path)
return full_path
def copy(filename, dirpath):
shutil.copy(os.path.join(srcpath, filename), dirpath)
# create destination directories and store their names along with full paths
targets = [
(folder, create(folder, destpath)) for folder in destdirs
]
for destdirs, full_path in targets:
for filename in srcfiles:
if destdirs == filename[0:3]:
copy(filename, full_path)
ERROR
Traceback (most recent call last):
File "C:/Users/Desktop/copy_only.py", line 45, in <module>
copy(filename, full_path)
File "C:/Users/Desktop/copy_only.py", line 35, in copy
shutil.copy(os.path.join(srcpath, filename), dirpath)
File "C:\Python27\lib\shutil.py", line 119, in copy
copyfile(src, dst)
File "C:\Python27\lib\shutil.py", line 82, in copyfile
with open(src, 'rb') as fsrc:
IOError: [Errno 13] Permission denied: 'C:\\temp\\Test\\F12'
ファイル名としてF12を開こうとしています。その理由は、私はlistdir.The監督のフォルダをフィルタリングしたいローカルコンピュータ上の完全な権利ですので、私はそれが許可の問題だとは思わない – Adam