How to Copy a File to Another Path

How to copy file from one location to another location?

You can use this (or any variant):

Files.copy(src, dst, StandardCopyOption.REPLACE_EXISTING);

Also, I'd recommend using File.separator or / instead of \\ to make it compliant across multiple OS, question/answer on this available here.

Since you're not sure how to temporarily store files, take a look at ArrayList:

List<File> files = new ArrayList();
files.add(foundFile);

To move a List of files into a single directory:

List<File> files = ...;
String path = "C:/destination/";
for(File file : files) {
Files.copy(file.toPath(),
(new File(path + file.getName())).toPath(),
StandardCopyOption.REPLACE_EXISTING);
}

How to copy a file to a specific folder in a Python script?

Use shutil.copy(filePath, folderPath) instead of shutil.copyfile(). This will allow you to specify a folder as the destination and copies the file including permissions.

shutil.copy(src, dst, *, follow_symlinks=True):

Copies the file src to the file or directory dst. src and dst should be strings. If dst specifies a directory, the file will be copied into dst using the base filename from src. Returns the path to the newly created file.

...

copy() copies the file data and the file’s permission mode (see os.chmod()). Other metadata, like the file’s creation and modification times, is not preserved. To preserve all file metadata from the original, use copy2() instead.

https://docs.python.org/3/library/shutil.html#shutil.copy

See the difference in copying also documented in shutil.copyfile() itself:

shutil.copyfile(src, dst, *, follow_symlinks=True):

Copy the contents (no metadata) of the file named src to a file named dst and return dst. src and dst are path names given as strings. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst specify the same file, SameFileError is raised.

https://docs.python.org/3/library/shutil.html#shutil.copyfile

Copy a file from one location to another in Python

You have to give a full name of the destination file, not just a folder name.

You can get the file name using os.path.basename(path) and then build the destination path using os.path.join(path, *paths)

for item in fileList:
filename = os.path.basename(item[0])
copyfile(item[0], os.path.join("/Users/username/Desktop/testPhotos", filename))

Copy file from a folder to another

Get the current dir path and add the src path and dest path to it.

Ex:

import os
import shutil

BASE_PATH = os.path.dirname(os.path.realpath(__file__))

shutil.copyfile(os.path.join(BASE_PATH, "..", "A", "copyme.txt"),
os.path.join(BASE_PATH, "B", "copyme.txt"))

How to copy file from directory to another Directory in Java

Use Apache Commons FileUtils
FileUtils.copyDirectory(source, desc);

how to copy selected file without moving it to another directory in python

You can use shutil.copyfile(src, dst)

import os
import shutil

cwd = os.getcwd()
filepath = os.path.dirname(os.path.abspath(filepath))
shutil.copyfile(os.path.join(filepath, filename), os.path.join(cwd, 'modeldata/temp/' filename))

Read more here:
https://docs.python.org/3/library/shutil.html



Related Topics



Leave a reply



Submit