user16256544
user16256544

Reputation:

Python copy contents of a file into another at a different directory

I am trying to copy contents of a file into another located at a different directory. I am trying it by 2 methods -

shutil.copy('abc.txt', '/projects/fldr/notes/work/sg1234/lib/test/main/xyz.txt')

Problem with this method is that there are different users, so I can't use sg1234 in path, I couldn't think of a way to change user so path becomes -

shutil.copy('abc.txt', '/projects/fldr/notes/work/{user}/lib/test/main/xyz.txt')

and if I am trying to do it like this then it is throwing an error.-

shutil.copy('abc.txt', '/../../../../../../../../xyz.txt')

I also tried -

file.open(output_file_path + 'xyz.txt')
shutil.copy('abc.txt', 'xyz.txt')

But in this case it is only creating an empty file.

How can I do this?

Upvotes: 2

Views: 54

Answers (1)

Corralien
Corralien

Reputation: 120559

import shutil
import getuser

user = getpass.getuser()

shutil.copy('abc.txt', f'/projects/fldr/notes/work/{user}/lib/test/main/xyz.txt')

Upvotes: 2

Related Questions