Home > Enterprise >  how to write file to another folder in python
how to write file to another folder in python

Time:10-25

I am encrypting a file and it encrypts to a folder named mark but I want the file to encrypt and be placed in a sub folder named Kevin. Code is below:

with open("encrypted_"   filename   '.txt', "wb") as ef:
    ef.write(encrypted_file)

That encrypts the file but places it in the same folder. How can i edit this so it moves to the Kevin folder after encryption. I am using Python.

Thanks

CodePudding user response:

import shutil

with open("encrypted_"   filename   '.txt', "wb") as ef:
    ef.write(encrypted_file)

shutil.move("encrypted_", "Kevin") #"encrypted_" is the file and "Kevin" is the Destination Folder

CodePudding user response:

Something like this should work:

import os 

# Create the folder
dir_name = "Kevin"
os.mkdir(dir_name)

# write the file in the folder
with open(dir_name   "/encrypted_"   filename   '.txt', "wb") as ef: 
    ef.write(encrypted_file)

CodePudding user response:

If using linux, try to move it after encryption:

import shutil
shutil.move("origin_path", "destination_path")
  • Related