user3541631
user3541631

Reputation: 4008

Copy file to a different location and changing the filename at the same time

I want to copy file to a different location and changing the filename at the same time:

from shutils import copyfile, copy
    path = os.path.join(dst, 'file_new_name.xls')
    copyfile(src, path) # or
    copy(src, path)

I get a FileNotFoundError with path : dst\old_filename\newfilename.

It happens because I try to copy as a different file name.

Upvotes: 1

Views: 215

Answers (2)

NotAdrian
NotAdrian

Reputation: 69

I guess the fastest way is to use this right here:

At first you import fs on top of your document with this line

var fs = require('fs');

And than you can use it like this to copy the file to the new Location and also Change the Name at the same time.

fs.createReadStream('FirstLocationFile.txt').pipe(fs.createWriteStream('SecondLocationFile.txt'));

You could of course also add Folder paths into the Strings to decribe if they are or shall be inside a Folder.

Upvotes: 0

blhsing
blhsing

Reputation: 106648

You should join the directory name of the old file path with the new file name instead:

path = os.path.join(os.path.dirname(dst), 'file_new_name.xls')

Upvotes: 1

Related Questions