Python Forum
how would you copy a file?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how would you copy a file?
#11
I think shutil has for a very long time been the preferred way to copy or move files is Python.
My answers 10-years ago(i have looked) would pretty much be the same today.
Could add pathlib to be more modern.
from pathlib import Path
from shutil import copy, move

source = Path('old_file.txt')
destination = Path('new_file.txt')
copy(source, destination)
# move(source, destination)
Reply
#12
do you remember how you learned that shutil was the best way to copy a file? i'm trying to figure out how long it would have taken me to find that w/o this forum. it seems like it would not be before the time of giving up. the name shutil seems like it mean "shell utilities". since i don't consider copying a file to be a shell function, i would not expect it to be in there. with so many places to look, i would focus on what would seem to be a more obvious place, such as a name like fileops or fscopy. but now i know. i will go read and see where i can use it so i can remember it.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#13
shutil.copytree() does not fulfill my recent needs, because it requires the destination tree to not exit. my need is to copy on tree to another, copying only where an individual file does not exist. creating directories as needed.

shutil.copystat() does not copy owner and group. i cannot find a function in that module that does.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#14
(Oct-01-2019, 06:52 PM)Skaperen Wrote: do you remember how you learned that shutil was the best way to copy a file?
shutil has been in Python standard library for very long time,think since Python 1.4.
(Oct-01-2019, 06:52 PM)Skaperen Wrote: the name shutil seems like it mean "shell utilities". since i don't consider copying a file to be a shell function, i would not expect it to be in there
It's build looking at how UNIX/Linux based systems dos it.
shutil.copy() is comparable to the cp command in UNIX based systems.
Can call cp a Linux shell command as many do,so shell utilities(shutil) is not so bad,but the name could have been better.

Quote:shutil.copytree() does not fulfill my recent needs, because it requires the destination tree to not exit.
In many case when is not a simple requirement,need to combine shutil with eg os,pathlib, catch the error(try-expect) do something else.
Search for solution's.
Reply
#15
Skaperen Wrote:shutil.copytree() does not fulfill my recent needs, because it requires the destination tree to not exit
pyfilesystem as several copying options that I think could suit your needs.
Reply
#16
my basic question concept was whether or not copying file contents was commonly done by invoking the platform's file copying program, or to run python code to read and write the contents. the answer seems to allow either choice. shutil apparently reads and writes contents ... i see no additional processes created. in theory it could have run another program. so the answer is "whatever shutil does because they made it for that purpose".

this reveals clearly that i have not learned the library well, at least in terms of mapping things to do to where to get the solution. i have been coding python for about 7 years. snippsat points out that shutil has been around since 1.4. that would put it at 25 Oct 1996, nearly 23 years ago. but how does that help learn it? it doesn't. a month ago i had heard of shutil but didn't know any reason to put it above other things i was learning. still, python was successful even when doing copying my own way (which i am used to doing).

what i need is (a reference to) a document that is indexed or organized by various things people need to do, with a description of the generally accepted way to do this in the python community. currently, i am jumping around in the library reference manual. sure, it's not for learning this, but what is?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#17
Skaperen Wrote:what i need is (a reference to) a document that is indexed or organized by various things people need to do

A search with the keywords 'python recipes' may yield plenty of interesting results here.
Reply
#18
got some nice results, with yummy pictures:

1. How to Cook Python Fillet

2. Recipe: Poached Burmese Python Curry

3. Poached garlic python steaks

below that were a bunch of links to code recipes. this one looks interesting: https://python-3-patterns-idioms-test.re...en/latest/
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  best way to copy a big binary file Skaperen 15 8,812 Sep-06-2019, 09:00 PM
Last Post: Skaperen

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020