Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
import shutil
#1
Hi,
Why the code not copy files from folder 111 to folder 222?

The code:
import shutil
shutil.copy('C:\111','C:\111')

Look at an example in the video:
https://youtu.be/_Qa0Ezm7D9c

Thanks

Beni,
Reply
#2
shutil.copy copies files, not directories.

Try copytree method: https://docs.python.org/3.7/library/shut...l.copytree
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#3
Hi,
I Want to copy file, not tree.
There is a mistake in the form I compile or in the code and I do not know what
Reply
#4
Well, how could you copy the file if the file name is the same? Notice the previous mtime of the file and the new one and you will see what is happening.
"As they say in Mexico 'dosvidaniya'. That makes two vidaniyas."
https://freedns.afraid.org
Reply
#5
(Nov-13-2018, 08:23 AM)beni Wrote: I Want to copy file, not tree.
Note that
1. there is no file name in your code
2. both source and target are the same

try
import shutil
shutil.copy(r'C:\111\AAA.PDF', r'C:\222\AAA.PDF')
And please, use proper tags when post code, traceback, output, etc.
See BBcode help for more info.
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
#6
Thank you! it works.
For what "r"?
Reply
#7
'r' denotes it is a raw string. Because you are using back-slash (\) and back-slash is used as escape char it may cause problems with your path. e.g. if you have \t this will be considered tab.

Unless an 'r' or 'R' prefix is present, escape sequences in string and bytes literals are interpreted according to rules similar to those used by Standard C. The recognized escape sequences are:
Escape Sequence Meaning Notes
\newline Backslash and newline ignored
\\ Backslash (\)
\' Single quote (')
\" Double quote (")
\a ASCII Bell (BEL)
\b ASCII Backspace (BS)
\f ASCII Formfeed (FF)
\n ASCII Linefeed (LF)
\r ASCII Carriage Return (CR)
\t ASCII Horizontal Tab (TAB)
\v ASCII Vertical Tab (VT)
\ooo Character with octal value ooo (1,3)
\xhh Character with hex value hh (2,3)


you can use forward-slash instead
shutil.copy('C:/111/AAA.PDF', 'C:/222/AAA.PDF')
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


Possibly Related Threads…
Thread Author Replies Views Last Post
  use of shutil.copytree with ENOTDIR exception yan 2 841 Nov-29-2023, 03:02 PM
Last Post: yan
  shutil.move make data corrupt kucingkembar 0 744 Feb-01-2023, 01:30 PM
Last Post: kucingkembar
  Merge htm files with shutil library (TypeError: 'module' object is not callable) Melcu54 5 1,494 Aug-28-2022, 07:11 AM
Last Post: Melcu54
  extra slashes in the network path for shutil.copy tester_V 3 3,695 Jun-02-2021, 07:57 AM
Last Post: supuflounder
  concatenat - shutil jmabrito 3 2,146 Feb-11-2021, 12:17 PM
Last Post: jmabrito
  Shutil FileNotFoundError: Errno 2 Help lord_kaiser 8 10,360 Aug-10-2020, 08:45 AM
Last Post: lord_kaiser
  shutil.copy questions kristianpython 3 2,253 Jul-14-2020, 09:19 AM
Last Post: Gribouillis
  Shutil move if file exists in destination Friend 2 6,651 Feb-02-2020, 01:45 PM
Last Post: Friend
  Shutil attempts to copy directories that don't exist ConsoleGeek 5 4,450 Oct-29-2019, 09:26 PM
Last Post: Gribouillis
  Help copying files with shutil hikerguy62 2 4,215 Aug-02-2019, 08:16 PM
Last Post: hikerguy62

Forum Jump:

User Panel Messages

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