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?
#1
suppose your project is to create a script to copy a list of files from A to B only if the file does not already exist at B. you would iterate over the list, and test if it exists at B. if it does not exist at B then you will copy the file. would you write code to open the source and destination files and read/write all the contents, or would you invoke the local file copy command to do 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
#2
if you use pathlib, testing if file exists is simple:
if filename.exists():
    ...
Reply
#3
Skaperen Wrote:would you write code to open the source and destination files and read/write all the contents
No I would use one of the shutil.copy functions or the pyfilesystem equivalent (module fs.copy) which I'm fond of.
Reply
#4
(Sep-29-2019, 07:35 PM)Larz60+ Wrote: if you use pathlib, testing if file exists is simple:
if filename.exists():
    ...

testing if the file exists is not the issue. i was just describing an example special case reason to build a tree copy. yeah, it is a poor example because cp and rsync both have options to do this, so such a script really isn't needed. what i am trying to focus on is of this were a needed case and you built such a script, when it gets down to copying each individual file, how would other python coders choose to do it? by invoking a copy command or by opening the files and reading and writing all the data?
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#5
Testing if the file exists means that you have to compare the hash sums of both files. It could be slow.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#6
wavic, yea, might take a few microseconds
Reply
#7
(Sep-30-2019, 04:30 AM)Skaperen Wrote: by invoking a copy command or by opening the files and reading and writing all the data?
why do you continue to ignore python tools in os, shutil, pathlib?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
(Sep-30-2019, 03:14 PM)Larz60+ Wrote: wavic, yea, might take a few microseconds

Depends on the file size. It's should have some load delay.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#9
(Sep-30-2019, 03:24 PM)buran Wrote:
(Sep-30-2019, 04:30 AM)Skaperen Wrote: by invoking a copy command or by opening the files and reading and writing all the data?
why do you continue to ignore python tools in os, shutil, pathlib?
i didn't say i was ignoring python tools. i don't yet know them all while as the same time i am trying to learn things from an internals perspective.

for example, i could copy a file by doing subprocess.call(['cp',fn1,fn2]). that command name might not exist everywhere. there is more than one way to do things in python. just because i know about one that is the "wrong" way to do things does not mean i am ignoring them. if i just don't know, yet, that's not ignoring.

the documentation is huge. it is not an easy straight read. and if i just don't code anything at all while reading it, i won't remember it, and i will, again, be accused of ignoring it.

can you point me to the concise document that lists the right way to do everything? it would need to be indexed by each thing to do in some order with the "answer" for each. "how to copy a file" would be one or more of these. there could be variations on copying files that might need a very different answer? and this document could be important in case there might be debates regarding the right way to do things in python.

where did you learn all these? did you read them somewhere? did you do any coding before you knew at least a significant number of them?

before i get to the point of knowing the right way to do everything in python, i would need to find out somewhere. all google does is, in most cases, prove that there is more than one way to do it. asking seems to not get more than vague answers or accusations. reading documentation is often like trying to navigate a maze.
Tradition is peer pressure from dead people

What do you call someone who speaks three languages? Trilingual. Two languages? Bilingual. One language? American.
Reply
#10
Skaperen Wrote:can you point me to the concise document that lists the right way to do everything? it would need to be indexed by each thing to do in some order with the "answer" for each. "how to copy a file" would be one or more of these. there could be variations on copying files that might need a very different answer? and this document could be important in case there might be debates regarding the right way to do things in python.
You know the fundamental theorem of software engineering
David Wheeler (presumably) Wrote:We can solve any problem by introducing an extra level of indirection.
Add a level of indirection by interposing your own library of solutions
from my_solutions import copy_file
...
copy_file(fn1, fn2, variant='cp')
This would allow you to enrich gradually your collection of solutions without impacting the rest of your code.
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