

{"id":19360,"date":"2018-06-20T04:00:49","date_gmt":"2018-06-19T22:30:49","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=19360"},"modified":"2026-04-20T16:07:16","modified_gmt":"2026-04-20T10:37:16","slug":"how-python-copy-a-file","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/","title":{"rendered":"How Python Copy a File &#8211; 9 Simple &amp; Quick Ways"},"content":{"rendered":"<p>In our last tutorial, we studied Python Zipfile. Today, in this Python Tutorial, we will discuss how Python copies a file.<\/p>\n<p>Moreover, we will look at the 9 simple ways to copy a file in Python Programming: Using Python OS Module, Python Threading Library, Python Subprocess Module, Python Shutil Module.<\/p>\n<p>So, let&#8217;s start with How to Copy a File in Python.<\/p>\n<h3>How Python Copy a File?<\/h3>\n<p>Here are the main 4 categories of ways through which Python copies a file.<\/p>\n<h3 class=\"western\">1. Using Python OS Module<\/h3>\n<p>There are two ways to copy a file in Python- the popen() method and the system() method. Let\u2019s discuss them.<\/p>\n<div id=\"attachment_19377\" style=\"width: 510px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module.png\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-19377\" class=\"wp-image-19377 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module.png\" alt=\"How Python Copy a File - 9 Simple &amp; Quick Ways\" width=\"500\" height=\"500\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module.png 500w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module-150x150.png 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module-300x300.png 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-OS-Module-100x100.png 100w\" sizes=\"auto, (max-width: 500px) 100vw, 500px\" \/><\/a><p id=\"caption-attachment-19377\" class=\"wp-caption-text\">How Python Copy a File &#8211; 9 Simple &amp; Quick Ways<\/p><\/div>\n<h4 class=\"western\">a. popen() in Python<\/h4>\n<p>The popen() method returns a file object that connects to a pipe. In mode \u2018w\u2019, we can write to it, and in mode \u2018r\u2019, we can read from it (\u2018r\u2019 is the default).<\/p>\n<p>You can say that it creates a pipe to a command or from it.<\/p>\n<p>This is the syntax we use:<\/p>\n<pre class=\"EnlighterJSRAW\">os.popen(command[, mode[, bufsize]])<\/pre>\n<p>Here, <i>command<\/i> is the command we use, and <i>mode<\/i> is \u2018r\u2019 or \u2018w\u2019, as discussed previously.<\/p>\n<p>When <i>bufsize<\/i> is 0, there is no buffering. When it is 1, there is line buffering when accessing a file.<\/p>\n<p>When it is an integer greater than 1, there is buffering with the indicated buffer size. For a negative value, a default buffer size is used.<\/p>\n<p>We have a file labeled \u20181.txt\u2019 on the Desktop. We use the following code:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; os.popen('copy 1.txt 2.txt')<\/pre>\n<p>As you can see, this creates a new file labeled \u20182.txt\u2019 on the Desktop. This has the same contents as 1.txt.<\/p>\n<p>For Linux, you\u2019d have to write \u2018cp\u2019 instead of \u2018copy\u2019. This method has been deprecated in Python 2.6.<\/p>\n<h4 class=\"western\">b. system() in Python<\/h4>\n<p>The system() method lets us execute a command as a string in a subshell. Any output the <i>command<\/i> generates goes to the interpreter&#8217;s standard output stream.<\/p>\n<p>The syntax is:<\/p>\n<pre class=\"EnlighterJSRAW\">os.system(command)<\/pre>\n<p>Let\u2019s try doing the same thing using system() here.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; os.system('copy 1.txt 2.txt')\r\n0<\/pre>\n<p>We can see the same action with this method. It copies the contents of a file 1.txt into a new file 2.txt.<\/p>\n<p>Note that if we already had a file called 2.txt, this operation replaces its contents. This applies to the previous method as well.<\/p>\n<p>While this method is similar to popen(), it executes in a subshell. It executes in a separate thread parallel to our executing code.<\/p>\n<p>By calling .wait() on the object it returns, we can wait for its completion.<\/p>\n<h3 class=\"western\">2.\u00a0 Using Python Threading Library<\/h3>\n<p>We can borrow the Thread module from the threading library to copy a file in Python. This does the copying in an async manner.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; import shutil\r\n&gt;&gt;&gt; from threading import Thread\r\n&gt;&gt;&gt; Thread(target=shutil.copy, args=['1.txt','2.txt']).start()<\/pre>\n<p>This code copies the contents of file 1.txt to a new file 2.txt.<\/p>\n<p>If your application uses multiple threads for reading or writing a file, you should employ locking to avoid deadlocks.<\/p>\n<h3 class=\"western\">3. Using Python Subprocess Module<\/h3>\n<p>The subprocess module lets us work with child processes- launch them, attach to their pipes for input, output, and error, and retrieve return values.<\/p>\n<p>From this module, we can use the methods call() and check_output(), to copy a file in Python.<\/p>\n<p>Let\u2019s see how this module helps Python copy a file.<\/p>\n<div id=\"attachment_19380\" style=\"width: 1090px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-19380\" class=\"wp-image-19380 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01.jpg\" alt=\"Python copy a file - Python Subprocess Module\" width=\"1080\" height=\"1080\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01.jpg 1080w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01-150x150.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01-300x300.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01-768x768.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01-1024x1024.jpg 1024w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-Subprocess-Module-01-100x100.jpg 100w\" sizes=\"auto, (max-width: 1080px) 100vw, 1080px\" \/><\/a><p id=\"caption-attachment-19380\" class=\"wp-caption-text\">Python Copy a File &#8211; Python Subprocess Module<\/p><\/div>\n<h4 class=\"western\">a. call() in Python<\/h4>\n<p>The syntax for this command is the following:<\/p>\n<pre class=\"EnlighterJSRAW\">subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None)<\/pre>\n<p>The Python call() method runs the command that the <i>args<\/i> describe. It waits for the command to complete and then returns the attribute <i>return code<\/i>.<\/p>\n<p>We use the following code to copy a file:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os, subprocess\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; status=subprocess.call('copy 1.txt 2.txt', shell=True)\r\n&gt;&gt;&gt; if status!=0:\r\n       if status&lt;0:\r\n            print(f\"Killed by signal {status}\")\r\n       else:\r\n            print(f\"Command failed with return code {status}\")\r\n       else:\r\n            print(\"Successfully executed command\")\r\n<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">Successfully executed command<\/div>\n<p>Since here we get the output we do, we figure that the code returns a 0.<\/p>\n<h4 class=\"western\">b. check_output() in Python<\/h4>\n<p>Now, let\u2019s try the same with check_output(). This method runs commands with arguments and returns the output.<\/p>\n<p>For a non-zero return code, it raises a CalledProcessError. It also supports pipes. This is the syntax to follow:<\/p>\n<p><strong>subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)<\/strong><\/p>\n<p>This method is a lot like subprocess.run, but it pipes data from stdout as encoded bytes by default. Let\u2019s see how we can copy a file with it.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os, subprocess\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; status=subprocess.check_output('copy 1.txt 2.txt',shell=True)<\/pre>\n<h3 class=\"western\">4. Using Python Shutil Module<\/h3>\n<p>Copying files by hand is slow. Python\u2019s shutil module offers a one-line fix: shutil.copy(&#8220;data.csv&#8221;, &#8220;backup\/data.csv&#8221;). It keeps the file name while moving its bytes, giving an instant backup. This \u201ccopy files in Python\u201d trick saves both time and typing.<\/p>\n<p><strong>Benefits of using the Shutil method in Python:<\/strong><\/p>\n<ul>\n<li><strong>Easy file operation:<\/strong> You can move and copy files and folders with the help of simple Shutil commands.<\/li>\n<li><strong>Good automation:<\/strong> It helps in reducing code, and it makes the code easy to write and understand.<\/li>\n<li><strong>System tools:<\/strong> You can check disk space with the help of shutil.disk_usage() or find programs using shutil.which().<\/li>\n<\/ul>\n<p>There are four methods we will consider in this module to help Python copy a file: copyfile(), copy(), copy2(), and copyfileobj().<\/p>\n<div id=\"attachment_19381\" style=\"width: 1210px\" class=\"wp-caption aligncenter\"><a href=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01.jpg\"><img loading=\"lazy\" decoding=\"async\" aria-describedby=\"caption-attachment-19381\" class=\"wp-image-19381 size-full\" src=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01.jpg\" alt=\"Python copy a file - Python shutil Modules\" width=\"1200\" height=\"628\" srcset=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01.jpg 1200w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01-150x79.jpg 150w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01-300x157.jpg 300w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01-768x402.jpg 768w, https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Using-shutil-Module-01-1024x536.jpg 1024w\" sizes=\"auto, (max-width: 1200px) 100vw, 1200px\" \/><\/a><p id=\"caption-attachment-19381\" class=\"wp-caption-text\">Python copy a file &#8211; Python shutil Modules<\/p><\/div>\n<h4 class=\"western\">a. copyfile() in Python<\/h4>\n<p>copyfile() copies one file\u2019s contents into another. For this, the target should be writable; otherwise, it raises an IOError. When the destination is a directory, it raises Error 13.<\/p>\n<p>And like we discussed previously, these methods replace a destination file if it already exists and throw an error if the source and destination names are the same.<\/p>\n<p>We have the following syntax for it:<\/p>\n<p><strong>shutil.copyfile(src_file, dest_file, *, follow_symlinks=True)<\/strong><\/p>\n<p>Let\u2019s try the following code:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os,shutil\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; shutil.copyfile('1.txt','2.txt')\r\n'2.txt'<\/pre>\n<p>This code gives us the name of the file it created.<\/p>\n<p>Also, when we set the <i>follow_symlinks<\/i> argument to False, it creates a symbolic link from the source file, which is a symbolic link, instead of copying it.<\/p>\n<h4 class=\"western\">b. copy() in Python<\/h4>\n<p>Like copyfile(), copy() lets us copy content from one file to another. However, this one also copies file system permissions.<\/p>\n<p>We have the following syntax:<\/p>\n<p><strong>shutil.copy(src_file, dest_file, *, follow_symlinks=True)<\/strong><\/p>\n<p>Let\u2019s try copying with this method too:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os,shutil\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; shutil.copy('1.txt','2.txt')\r\n'2.txt'<\/pre>\n<p>So then, what is different here from copyfile()? Here we go:<\/p>\n<ol>\n<li>copyfile() copies data, copy() also copies permission bits<\/li>\n<li>copy() can copy to directories, copyfile() raises an Error 13 instead<\/li>\n<li>copyfile() uses method copyfileobj() in its implementation, but copy uses functions copyfile() and copymode() in turn<\/li>\n<li>The last aspect makes copy() slower than copyfile(), which doesn\u2019t have the overhead of preserving permissions<\/li>\n<\/ol>\n<h4 class=\"western\">c. copy2() in Python<\/h4>\n<p>This one\u2019s like copy(), but while copying the data, it also gets access and modification times linked to the metadata.<\/p>\n<p>For platforms that do not allow for full metadata saving, it preserves any metadata it can.<\/p>\n<p>We have the following syntax:<\/p>\n<p><strong>shutil.copy2(src_file, dest_file, *, follow_symlinks=True)<\/strong><\/p>\n<p>Now let\u2019s hit the payload.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os,shutil\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; shutil.copy2('1.txt','2.txt')\r\n'2.txt'<\/pre>\n<p>This method preserves the creation date. Let\u2019s see the differences between copy() and copy2().<\/p>\n<ol>\n<li>copy() sets permission bits, copy2() also updates file metadata with timestamps<\/li>\n<li>Internally, copy() calls copyfile() and copymode(); copy2() calls copyfile() and copystat()<\/li>\n<\/ol>\n<h4 class=\"western\">d. copyfileobj() in Python<\/h4>\n<p>If you have been reading from your source file object, copyfileobj() will start copying from the position you stopped reading at.<\/p>\n<p>It has the following syntax:<\/p>\n<p><strong>shutil.copyfileobj(src_file_object, dest_file_object[, length])<\/strong><\/p>\n<p>Here, source and destination file parameters refer to objects. <i>length<\/i> is the buffer size. This is the number of bytes in memory during a copy.<\/p>\n<p>This comes in handy when copying very large files, and the default is 16KB. Let\u2019s copy using this last method.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os,shutil\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; one=open('1.txt','rb')\r\n&gt;&gt;&gt; two=open('2.txt','wb')\r\n&gt;&gt;&gt; shutil.copyfileobj(one,two)<\/pre>\n<p>Remember to open the files in binary mode. Open the source file as readable and the destination file as writable.<\/p>\n<p>Also, remember to close the files once you\u2019re done.<\/p>\n<p>So, this was all about How Python Copy a File. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Copy a File<\/h3>\n<p>1. How do you copy a file in Python?<\/p>\n<p>2. How do you copy a file from one directory to another in Python?<\/p>\n<p>3. How do you use Shutil Copy in Python?<\/p>\n<p>4. What does Shutil mean in Python?<\/p>\n<p>5. How does Shutil copy work in Python?<\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Hence, in this tutorial, we discussed nine ways for Python to copy a file.<\/p>\n<p>Moreover, we discussed the commands in the Python OS Module, Python Threading Library, Python Subprocess Module, and Shutil Module in Python.<\/p>\n<p>Which one do you prefer? Comment below.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>In our last tutorial, we studied Python Zipfile. Today, in this Python Tutorial, we will discuss how Python copies a file. Moreover, we will look at the 9 simple ways to copy a file&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":19372,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[146,2274,2493,2994,2995,2996,2997,5932,9559,10447,14127,15297,15298,15299,15300],"class_list":["post-19360","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-9-ways-to-copy-a-file-in-python","tag-call","tag-check_output","tag-copy","tag-copy2","tag-copyfile","tag-copyfileobj","tag-how-python-copy-a-file","tag-popen","tag-python-copy-file","tag-system","tag-using-python-os-module","tag-using-python-shutil-module","tag-using-python-subprocess-module","tag-using-python-threading-library"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.4 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How Python Copy a File - 9 Simple &amp; Quick Ways - DataFlair<\/title>\n<meta name=\"description\" content=\"How Python Copy a File- Using Python OS Module,Python Threading Library,Python Subprocess Module,Python Shutil Module,open(),system(),call(),check_output()\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Python Copy a File - 9 Simple &amp; Quick Ways - DataFlair\" \/>\n<meta property=\"og:description\" content=\"How Python Copy a File- Using Python OS Module,Python Threading Library,Python Subprocess Module,Python Shutil Module,open(),system(),call(),check_output()\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/\" \/>\n<meta property=\"og:site_name\" content=\"DataFlair\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/DataFlairWS\/\" \/>\n<meta property=\"article:published_time\" content=\"2018-06-19T22:30:49+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T10:37:16+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1200\" \/>\n\t<meta property=\"og:image:height\" content=\"628\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"DataFlair Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:site\" content=\"@DataFlairWS\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"DataFlair Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Python Copy a File - 9 Simple &amp; Quick Ways - DataFlair","description":"How Python Copy a File- Using Python OS Module,Python Threading Library,Python Subprocess Module,Python Shutil Module,open(),system(),call(),check_output()","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/","og_locale":"en_US","og_type":"article","og_title":"How Python Copy a File - 9 Simple &amp; Quick Ways - DataFlair","og_description":"How Python Copy a File- Using Python OS Module,Python Threading Library,Python Subprocess Module,Python Shutil Module,open(),system(),call(),check_output()","og_url":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-06-19T22:30:49+00:00","article_modified_time":"2026-04-20T10:37:16+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg","type":"image\/jpeg"}],"author":"DataFlair Team","twitter_card":"summary_large_image","twitter_creator":"@DataFlairWS","twitter_site":"@DataFlairWS","twitter_misc":{"Written by":"DataFlair Team","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"How Python Copy a File &#8211; 9 Simple &amp; Quick Ways","datePublished":"2018-06-19T22:30:49+00:00","dateModified":"2026-04-20T10:37:16+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/"},"wordCount":1407,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg","keywords":["9 Ways to copy a file in python","call()","check_output()","copy()","copy2()","copyfile()","copyfileobj()","How Python Copy a File","popen()","Python Copy File","system()","Using Python OS Module","Using Python Shutil Module","Using Python Subprocess Module","Using Python Threading Library"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/","url":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/","name":"How Python Copy a File - 9 Simple &amp; Quick Ways - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg","datePublished":"2018-06-19T22:30:49+00:00","dateModified":"2026-04-20T10:37:16+00:00","description":"How Python Copy a File- Using Python OS Module,Python Threading Library,Python Subprocess Module,Python Shutil Module,open(),system(),call(),check_output()","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/How-Python-Copy-a-File-01.jpg","width":1200,"height":628,"caption":"How Python Copy a File - 9 Simple &amp; Quick Ways"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/how-python-copy-a-file\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Blog Home","item":"https:\/\/data-flair.training\/blogs\/"},{"@type":"ListItem","position":2,"name":"Python Tutorials","item":"https:\/\/data-flair.training\/blogs\/category\/python\/"},{"@type":"ListItem","position":3,"name":"How Python Copy a File &#8211; 9 Simple &amp; Quick Ways"}]},{"@type":"WebSite","@id":"https:\/\/data-flair.training\/blogs\/#website","url":"https:\/\/data-flair.training\/blogs\/","name":"DataFlair","description":"Learn Today. Lead Tomorrow.","publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/data-flair.training\/blogs\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/data-flair.training\/blogs\/#organization","name":"DataFlair","url":"https:\/\/data-flair.training\/blogs\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2016\/07\/Data-Flair.png","width":106,"height":48,"caption":"DataFlair"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/DataFlairWS\/","https:\/\/x.com\/DataFlairWS","https:\/\/www.linkedin.com\/company\/dataflair-web-services-pvt-ltd\/","https:\/\/www.youtube.com\/user\/DataFlairWS"]},{"@type":"Person","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823","name":"DataFlair Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/4cf3a74600d131330b8c481d519afd1574093ed89f6d3396a95393ad223eb7cd?s=96&d=mm&r=g","caption":"DataFlair Team"},"description":"DataFlair Team creates expert-level guides on programming, Java, Python, C++, DSA, AI, ML, data Science, Android, Flutter, MERN, Web Development, and technology. Our goal is to empower learners with easy-to-understand content. Explore our resources for career growth and practical learning.","url":"https:\/\/data-flair.training\/blogs\/author\/dfteam1\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/19360","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/comments?post=19360"}],"version-history":[{"count":13,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/19360\/revisions"}],"predecessor-version":[{"id":147718,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/19360\/revisions\/147718"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/19372"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=19360"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=19360"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=19360"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}