Issue
I want to perform a move operation on a Folder from one location to the other.
And this folder contains subfolders again within itself, I need to move the very first Subfolder to the destination.
Also re-iterating that I need to move an entire folder not Files.
Solution
The easiest is with pathlib
Suppose you have these two folders:
ls -1l
total 0
drwxr-xr-x 2 dawg wheel 64 Dec 23 08:12 202109
drwxr-xr-x 2 dawg wheel 64 Dec 23 08:12 202110
With pathlib you can do:
ur_root='/tmp/test'
from pathlib import Path
from shutil import move
p=Path(ur_root)
for fn in p.glob('*'):
print(fn)
Prints:
/tmp/test/202109
/tmp/test/202110
To only get 'the first' (which is OS dependent):
first=next(p.glob('*'))
If you want to sort the names (if you OS is delivering some other order):
>>> sorted(p.glob('*'), key=lambda fn: fn.name)
[PosixPath('/tmp/test/202109'), PosixPath('/tmp/test/202110')]
Or by modification time:
>>> sorted(p.glob('*'), key=lambda fn: fn.stat().st_mtime)
[PosixPath('/tmp/test/202109'), PosixPath('/tmp/test/202110')]
Then use shutil.move to move it.
Answered By - dawg
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.