3 from .common
import PostProcessor
4 from ..compat
import shutil
11 class MoveFilesAfterDownloadPP(PostProcessor
):
13 def __init__(self
, downloader
=None, downloaded
=True):
14 PostProcessor
.__init
__(self
, downloader
)
15 self
._downloaded
= downloaded
22 dl_path
, dl_name
= os
.path
.split(info
['filepath'])
23 finaldir
= info
.get('__finaldir', dl_path
)
24 finalpath
= os
.path
.join(finaldir
, dl_name
)
26 info
['__files_to_move'][info
['filepath']] = finalpath
28 make_newfilename
= lambda old
: os
.path
.join(finaldir
, os
.path
.basename(old
))
29 for oldfile
, newfile
in info
['__files_to_move'].items():
31 newfile
= make_newfilename(oldfile
)
32 if os
.path
.abspath(oldfile
) == os
.path
.abspath(newfile
):
34 if not os
.path
.exists(oldfile
):
35 self
.report_warning(f
'File "{oldfile}" cannot be found')
37 if os
.path
.exists(newfile
):
38 if self
.get_param('overwrites', True):
39 self
.report_warning(f
'Replacing existing file "{newfile}"')
43 f
'Cannot move file "{oldfile}" out of temporary directory since "{newfile}" already exists. ')
45 make_dir(newfile
, PostProcessingError
)
46 self
.to_screen(f
'Moving file "{oldfile}" to "{newfile}"')
47 shutil
.move(oldfile
, newfile
) # os.rename cannot move between volumes
49 info
['filepath'] = finalpath