user2023
user2023

Reputation: 478

Moving files conditionally using shutil

I am trying to move the files from a source directory to destination Dir in linux which are ending with .text extension with the timestamp using shutil but somewhat not working correctly, looks like i'm doing something wrong.

Below is the source Directory where files are:

$ ls -l /samba/smb_out/
-rw-r-----. 1 user1 tam 266852 Dec 16 21:41 fostert.20201215.text
-rw-r-----. 1 user1 tam 266852 Dec 16 21:41 fostert.20201216.text

test code:

>>> tm_src = time.strftime("%Y%m%d")
>>> tm_dst = time.strftime("%Y%m%d-%H:%M:%S")
>>> src = "/samba/smb_out/"
>>> dst = "/samba/script_logs/"
>>> outfile = ( src + tm_src + ".text")
>>> text_files = [el for el in os.listdir(src) if el.endswith(".text") and path.isfile(path.join(src, el))]
>>> for file in text_files:
    shutil.move(path.join(src, file ), dst + "-" + tm_dst + ".log")
    
'/samba/script_logs/-20201216-21:32:59.log'
'/samba/script_logs/-20201216-21:32:59.log'

desired Should be:

'/samba/script_logs/fostert.20201215.text-20201215-21:32:59.log'
'/samba/script_logs/fostert.20201215.text-20201216-21:32:59.log'

any idea what i'm doing wrong here.

Upvotes: 0

Views: 52

Answers (1)

Karn Kumar
Karn Kumar

Reputation: 8816

Below should work for you, you just missed the file from your for loop.

>>> for file in text_files:
      shutil.move(path.join(src, file ), dst + file + "-" + tm_dst + ".log")

Upvotes: 1

Related Questions