{"id":1540,"date":"2019-02-27T02:18:45","date_gmt":"2019-02-27T02:18:45","guid":{"rendered":"http:\/\/kusuaks7\/?p=1145"},"modified":"2023-08-25T10:45:53","modified_gmt":"2023-08-25T10:45:53","slug":"ten-python-file-system-methods-you-should-know","status":"publish","type":"post","link":"https:\/\/www.experfy.com\/blog\/bigdata-cloud\/ten-python-file-system-methods-you-should-know\/","title":{"rendered":"Ten Python File System Methods You Should Know"},"content":{"rendered":"<section name=\"7e76\">\n<p id=\"a17e\" name=\"a17e\">The file system is a bit like a house. Say you&rsquo;re spring cleaning and you need to move boxes of notebooks from one room to another.<\/p>\n<figure id=\"3d87\" name=\"3d87\">\n<p><canvas height=\"48\" width=\"75\"><\/canvas><img decoding=\"async\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*QgeHuSXEDs1iE-KQWQg8PA.jpeg\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*QgeHuSXEDs1iE-KQWQg8PA.jpeg\" \/><\/p>\n<\/figure>\n<p name=\"6803\" style=\"text-align: center;\"><span style=\"font-size:11px;\">Directories are like boxes. They hold&nbsp;things.<\/span><\/p>\n<p id=\"6803\" name=\"6803\">The boxes are like directories. They hold things. In this case, notebooks.<\/p>\n<figure id=\"6fb1\" name=\"6fb1\">\n<p><canvas height=\"57\" width=\"75\"><\/canvas><img decoding=\"async\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*ckHy431HWhCo7-6vGpqMHA.jpeg\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*ckHy431HWhCo7-6vGpqMHA.jpeg\" \/><\/p>\n<\/figure>\n<p id=\"2a83\" name=\"2a83\">The notebooks are like files. You can read and write to them. You can put them in your directory boxes.<\/p>\n<p id=\"c9ec\" name=\"c9ec\">Capiche?<\/p>\n<\/section>\n<section name=\"a388\">\n<hr \/>\n<p id=\"13eb\" name=\"13eb\">In this guide we&rsquo;ll look at methods from the&nbsp;<em>os<\/em>&nbsp;and&nbsp;<em>shutil<\/em>&nbsp;modules. The&nbsp;<a data-href=\"https:\/\/docs.python.org\/3\/library\/os.html\" href=\"https:\/\/docs.python.org\/3\/library\/os.html\" rel=\"noopener noreferrer\" target=\"_blank\"><em>os<\/em><\/a>module is the primary Python module for interacting with the operating system. The&nbsp;<a data-href=\"https:\/\/docs.python.org\/3\/library\/shutil.html#module-shutil\" href=\"https:\/\/docs.python.org\/3\/library\/shutil.html#module-shutil\" rel=\"noopener noreferrer\" target=\"_blank\"><em>shutil<\/em><\/a>&nbsp;module also contains high-level file operations. For some reason you make directories with&nbsp;<em>os<\/em>&nbsp;but move and copy them with&nbsp;<em>shutil<\/em>. Go figure.&nbsp;<\/p>\n<h4 id=\"0ebd\" name=\"0ebd\">Update: pathlib discussion added Feb. 16,&nbsp;2019<\/h4>\n<p id=\"f2dc\" name=\"f2dc\">In Python 3.4 the&nbsp;<a data-href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html\" href=\"https:\/\/docs.python.org\/3\/library\/pathlib.html\" rel=\"noopener noreferrer\" target=\"_blank\"><em>pathlib<\/em><\/a>&nbsp;module was added to the standard library to improve working with file paths, and as of 3.6 is plays nicely with the rest of the standard library. The&nbsp;<em>pathlib<\/em>&nbsp;methods provide some benefits for parsing file paths over the methods we&rsquo;ll discuss below &mdash; namely&nbsp;<em>pathlib&nbsp;<\/em>treats paths as objects rather than strings. Although&nbsp;<em>pathlib<\/em>&nbsp;is handy, it doesn&rsquo;t have all the lower level functionality we&rsquo;ll be exploring. Also, you&rsquo;ll undoubtedly see the&nbsp;<em>os<\/em>and&nbsp;<em>shutil<\/em>&nbsp;methods below in code for years to come. So it&rsquo;s definitely a good idea to be familiar with them.<\/p>\n<p id=\"e30e\" name=\"e30e\">I plan to discuss&nbsp;<em>pathlib<\/em>&nbsp;in a future article, so follow me to make sure you don&rsquo;t miss it. To learn more about the&nbsp;<em>pathlib<\/em>&nbsp;module now, see this&nbsp;<a data-href=\"https:\/\/realpython.com\/python-pathlib\/\" href=\"https:\/\/realpython.com\/python-pathlib\/\" rel=\"noopener noreferrer\" target=\"_blank\">article<\/a>&nbsp;and this&nbsp;<a data-href=\"https:\/\/pbpython.com\/pathlib-intro.html\" href=\"https:\/\/pbpython.com\/pathlib-intro.html\" rel=\"noopener noreferrer\" target=\"_blank\">article<\/a>.<\/p>\n<p id=\"e0f2\" name=\"e0f2\">A few other things to know before we dig in:<\/p>\n<ul>\n<li id=\"6b9c\" name=\"6b9c\">This guide is designed for Python 3. Python 2 won&rsquo;t be supported beyond&nbsp;<a data-href=\"https:\/\/github.com\/python\/devguide\/pull\/344\" href=\"https:\/\/github.com\/python\/devguide\/pull\/344\" rel=\"noopener noreferrer\" target=\"_blank\">Jan. 1, 2020<\/a>.<\/li>\n<li id=\"be38\" name=\"be38\">You need to import&nbsp;<em>os<\/em>&nbsp;and&nbsp;<em>shutil<\/em>&nbsp;into your file to use these commands.<\/li>\n<li id=\"3b88\" name=\"3b88\">My example code is available on&nbsp;<a data-href=\"https:\/\/github.com\/discdiver\/os-examples\" href=\"https:\/\/github.com\/discdiver\/os-examples\" rel=\"noopener noreferrer\" target=\"_blank\">GitHub<\/a>.<\/li>\n<li id=\"1c55\" name=\"1c55\">Substitute your own arguments for the arguments in quotes below.<\/li>\n<\/ul>\n<p id=\"c38e\" name=\"c38e\">Now that we&rsquo;ve got the context out of the way, let&rsquo;s get to it! Here&rsquo;s the list of 10 commands you should know.<\/p>\n<h3 id=\"0be5\" name=\"0be5\">10 File System&nbsp;Methods<\/h3>\n<p id=\"03a4\" name=\"03a4\">The list below follows this pattern:<\/p>\n<p id=\"0c5e\" name=\"0c5e\"><strong>Method &mdash; Description &mdash; Equivalent macOS Shell Command<\/strong><\/p>\n<h4 id=\"143d\" name=\"143d\">Get Info<\/h4>\n<ul>\n<li id=\"a4ca\" name=\"a4ca\"><code><strong>os.getcwd()<\/strong><\/code> &mdash; get the current working directory path as a string &mdash; <code>pwd<\/code><\/li>\n<li id=\"05fc\" name=\"05fc\"><code><strong>os.listdir()<\/strong><\/code> &mdash; get the contents of the current working directory as a list of strings &mdash; <code>ls<\/code><\/li>\n<li id=\"f919\" name=\"f919\"><code><strong>os.walk(&quot;starting_directory_path&quot;)<\/strong><\/code>&mdash; returns a generator with name and path info for directories and files in the the current directory and all subdirectories&mdash; no exact short CLI equivalent, but&nbsp;<code>ls -R<\/code>&nbsp;provides subdirectory names and the names of files within subdirectories<\/li>\n<\/ul>\n<h4 id=\"83a6\" name=\"83a6\">Change Things<\/h4>\n<ul>\n<li id=\"50d5\" name=\"50d5\"><code><strong>os.chdir(&quot;\/absolute\/or\/relative\/path&quot;)<\/strong><\/code> &mdash; change the current working directory &mdash; <code>cd<\/code><\/li>\n<li id=\"c7a3\" name=\"c7a3\"><code><strong>os.path.join()<\/strong><\/code>&mdash;create a path for later use &mdash; no short CLI equivalent<\/li>\n<li id=\"dfcc\" name=\"dfcc\"><code><strong>os.makedirs(&quot;dir1\/dir2&quot;)<\/strong><\/code><strong> <\/strong>&mdash; make directory &mdash;<code>mkdir -p<\/code><\/li>\n<li id=\"394b\" name=\"394b\"><code><strong>shutil.copy2(&quot;source_file_path&quot;, &quot;destination_directory_path&quot;)<\/strong><\/code> &mdash; copy a file or directory &mdash; <code>cp<\/code><\/li>\n<li id=\"d07d\" name=\"d07d\"><code><strong>shutil.move(&quot;source_file_path<em>&quot;<\/em>, &quot;destination_directory_path&quot;)<\/strong><\/code><strong> <\/strong>&mdash; move a file or directory &mdash; <code>mv<\/code><\/li>\n<li id=\"53c5\" name=\"53c5\"><code><strong>os.remove(&quot;my_file_path&quot;)<\/strong><\/code> &mdash; remove a file &mdash; <code>rm<\/code><\/li>\n<li id=\"5374\" name=\"5374\"><code><strong>shutil.rmtree(&quot;my_directory_path&quot;)<\/strong><\/code>&mdash; remove a directory and all files and directories in it &mdash;<code>rm -rf<\/code><\/li>\n<\/ul>\n<p id=\"5f62\" name=\"5f62\">Let&rsquo;s discuss.<\/p>\n<h3 id=\"72e9\" name=\"72e9\">Get Info<\/h3>\n<p id=\"8e74\" name=\"8e74\"><code><strong>os.getcwd()<\/strong><\/code><\/p>\n<p id=\"e699\" name=\"e699\">os.getcwd() returns the current working directory as a string. That one is straightforward.&nbsp;<\/p>\n<p id=\"b887\" name=\"b887\"><code><strong>os.listdir()<\/strong><\/code><\/p>\n<p id=\"3688\" name=\"3688\">os.listdir() returns the contents of the current working directory as a list of strings. That one is also straightforward.&nbsp;<\/p>\n<p id=\"edbe\" name=\"edbe\"><code><strong>os.walk(&quot;my_start_directory&quot;)<\/strong><\/code><\/p>\n<p id=\"2b0f\" name=\"2b0f\"><code>os.walk()<\/code>&nbsp;creates a generator that can return information about the current directory and subdirectories. It works through the directories in the specified starting directory.<\/p>\n<p id=\"95cf\" name=\"95cf\"><code>os.walk()<\/code>&nbsp;returns the following items for each directory it traverses:<\/p>\n<ol>\n<li id=\"4adb\" name=\"4adb\">current directory path as a string<\/li>\n<li id=\"c7a4\" name=\"c7a4\">subdirectory names in the current directory as lists of strings<\/li>\n<li id=\"f1b9\" name=\"f1b9\">filenames in current directory as a list of strings<\/li>\n<\/ol>\n<p id=\"ccd9\" name=\"ccd9\">It does this for each directory!<\/p>\n<p id=\"7bce\" name=\"7bce\">It&rsquo;s often useful to use&nbsp;<code>os.walk()<\/code>&nbsp;with a&nbsp;<em>for<\/em>&nbsp;loop to iterate over the contents of a directory and its subdirectories. For example, the following code will print all files in the directories and subdirectories of the current working directory.<\/p>\n<div id=\"beca\" name=\"beca\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\">import os<\/span><\/span><\/div>\n<div name=\"f45e\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\">cwd<\/span><\/span><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\"> = os.getcwd()<\/span><\/span><\/div>\n<div name=\"c9e3\"><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\">for dir_path, dir_names, file_names in os.walk(<\/span><\/span><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\">cwd<\/span><\/span><span style=\"font-family:courier new,courier,monospace;\"><span style=\"background-color:#D3D3D3;\">):<\/span><br \/>\n<span style=\"background-color:#D3D3D3;\">&nbsp;&nbsp;&nbsp; for f in file_names:<\/span><br \/>\n<span style=\"background-color:#D3D3D3;\">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; print(f)<\/span><\/span><\/div>\n<p id=\"fc47\" name=\"fc47\">That&rsquo;s how we get info, now let&rsquo;s look at commands that change the working directory or move, copy, or delete parts of the file system.<\/p>\n<figure id=\"a1fa\" name=\"a1fa\">\n<p><canvas height=\"48\" width=\"75\"><\/canvas><img decoding=\"async\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*aZRPs1GPDwtdLYmysQbOjg.jpeg\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*aZRPs1GPDwtdLYmysQbOjg.jpeg\" \/><\/p>\n<\/figure>\n<h3 id=\"1650\" name=\"1650\">Change Things<\/h3>\n<p id=\"9960\" name=\"9960\"><code><strong>os.chdir(&quot;\/absolute\/or\/relative\/path&quot;)<\/strong><\/code><\/p>\n<p id=\"27cc\" name=\"27cc\">This method changes the current working directory to either the absolute or relative path provided.<\/p>\n<p id=\"1ab8\" name=\"1ab8\">If your code then makes other changes to the file system, it&rsquo;s a good idea to handle any exceptions raised when using this method with try-except. Otherwise you might be deleting directories or files you don&rsquo;t want deleted.&nbsp;<\/p>\n<p id=\"7316\" name=\"7316\"><code><strong>os.path.join()<\/strong><\/code><\/p>\n<p id=\"20db\" name=\"20db\">The&nbsp;<code><a data-href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#module-os.path\" href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#module-os.path\" rel=\"noopener noreferrer\" target=\"_blank\">os.path<\/a><\/code>&nbsp;module has a number of useful methods for common pathname manipulations. You can use it to find information about directory names and parts of directory names. The module also has methods to check whether a file or directory exists.<\/p>\n<p id=\"5665\" name=\"5665\"><code>os.path.join()<\/code>&nbsp;is designed to create a path that will work on most any operating system by joining multiple strings into one beautiful file path.<\/p>\n<p id=\"b868\" name=\"b868\">Here&rsquo;s the description from the&nbsp;<a data-href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#module-os.path\" href=\"https:\/\/docs.python.org\/3\/library\/os.path.html#module-os.path\" rel=\"noopener noreferrer\" target=\"_blank\">docs<\/a>:<\/p>\n<blockquote id=\"c693\" name=\"c693\"><p>Join one or more path components intelligently. The return value is the concatenation of&nbsp;<em>path<\/em>&nbsp;and any members of&nbsp;<em>*paths<\/em>&nbsp;with exactly one directory separator (<code><em>os.sep<\/em><\/code>) following each non-empty part except the last&hellip;<\/p><\/blockquote>\n<p id=\"0ad8\" name=\"0ad8\">Basically, if you are on a Unix or macOS system,&nbsp;<code>os.path.join()<\/code>&nbsp;sticks a forward slash (&ldquo;\/&rdquo;) between each string you provide to create a path. If the operating system needs a &ldquo;&rdquo; instead, then&nbsp;<em>join<\/em>&nbsp;knows to use a back slash.<\/p>\n<p id=\"8011\" name=\"8011\"><code>os.path.join()<\/code>&nbsp;also&nbsp;<a data-href=\"https:\/\/stackoverflow.com\/a\/13944874\/4590385\" href=\"https:\/\/stackoverflow.com\/a\/13944874\/4590385\" rel=\"noopener noreferrer\" target=\"_blank\">provides clear information<\/a>&nbsp;to other developers that you are creating a path. Definitely use it instead of manual string concatenation to avoid looking like a rookie.&nbsp;<\/p>\n<p id=\"bf12\" name=\"bf12\"><code><strong>os.makedirs(&quot;dir1\/dir2&quot;)<\/strong><\/code><\/p>\n<p id=\"57d5\" name=\"57d5\"><code>os.makedirs()<\/code>&nbsp;makes directories. The&nbsp;<code>mkdir()<\/code>&nbsp;method also makes directories, but it does not make intermediate directories. So I suggest you use&nbsp;<code>os.makedirs()<\/code>.<\/p>\n<p id=\"a994\" name=\"a994\"><code><strong>shutil.copy2(&quot;source_file&quot;, &quot;destination&quot;)<\/strong><\/code><\/p>\n<p id=\"e172\" name=\"e172\">There are many ways to copy files and directories in Python.&nbsp;<code>shutil.copy2()<\/code>is a good choice because it tries to preserve as much of the source file&rsquo;s metadata as possible. For more discussion, see&nbsp;<a data-href=\"https:\/\/stackabuse.com\/how-to-copy-a-file-in-python\/\" href=\"https:\/\/stackabuse.com\/how-to-copy-a-file-in-python\/\" rel=\"noopener noreferrer\" target=\"_blank\">this article<\/a>.<\/p>\n<figure id=\"49b2\" name=\"49b2\">\n<p><canvas height=\"75\" width=\"75\"><\/canvas><img decoding=\"async\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*ulcCBBppQsv5cOtEEnGxGA.jpeg\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*ulcCBBppQsv5cOtEEnGxGA.jpeg\" \/><\/p>\n<\/figure>\n<p name=\"4197\" style=\"text-align: center;\"><span style=\"font-size:11px;\">Move things<\/span><\/p>\n<p id=\"4197\" name=\"4197\"><code><strong>shutil.move(&quot;source_file<em>&quot;<\/em>, &quot;destination&quot;)<\/strong><\/code><\/p>\n<p id=\"d597\" name=\"d597\">Use&nbsp;<code>shutil.move()<\/code>&nbsp;to change a file&rsquo;s location. It uses&nbsp;<code>copy2<\/code>&nbsp;as the default under the hood.<\/p>\n<p id=\"10fe\" name=\"10fe\"><code><strong>os.remove(&quot;my_file_path&quot;)<\/strong><\/code><\/p>\n<p id=\"4467\" name=\"4467\">Sometimes you need to remove a file.&nbsp;<code>os.remove()<\/code>&nbsp;is your tool.<\/p>\n<p id=\"2e98\" name=\"2e98\"><code><strong>shutil.rmtree(&quot;my_directory_path&quot;)<\/strong><\/code><\/p>\n<p id=\"d28a\" name=\"d28a\"><code>shutil.rmtree()<\/code>&nbsp;removes a directory and all files and directories in it<\/p>\n<figure id=\"6a5c\" name=\"6a5c\">\n<p><canvas height=\"50\" width=\"75\"><\/canvas><img decoding=\"async\" data-src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*z7yyrOwHXFunBLfR0HGxCA.jpeg\" src=\"https:\/\/cdn-images-1.medium.com\/max\/1600\/1*z7yyrOwHXFunBLfR0HGxCA.jpeg\" \/><\/p><figcaption>&nbsp;<\/figcaption><\/figure>\n<p name=\"659e\" style=\"text-align: center;\"><span style=\"font-size:11px;\">Remove things<\/span><\/p>\n<p id=\"659e\" name=\"659e\">Careful with functions that delete things! You may want to print what will be deleted as a dry run with print(). Then run substitute in your remove function for print() when you&rsquo;re sure it won&rsquo;t delete the wrong files. Hat tip to Al Sweigart for that idea in&nbsp;<a data-href=\"https:\/\/automatetheboringstuff.com\/chapter9\/\" href=\"https:\/\/automatetheboringstuff.com\/chapter9\/\" rel=\"noopener noreferrer\" target=\"_blank\">Automate the Boring Stuff with Python<\/a>.<\/p>\n<p id=\"96cf\" name=\"96cf\">Here&rsquo;s the full list one more time.<\/p>\n<h3 id=\"7f8c\" name=\"7f8c\">10 File System Methods&nbsp;Recap<\/h3>\n<p id=\"17dd\" name=\"17dd\">The list below follows this pattern:&nbsp;<strong>Method&mdash; Description &mdash; Equivalent macOS Shell Command<\/strong><\/p>\n<h4 id=\"4b6d\" name=\"4b6d\">Get Info<\/h4>\n<ul>\n<li id=\"d950\" name=\"d950\"><code><strong>os.getcwd()<\/strong><\/code> &mdash; get the current working directory path as a string &mdash; <code>pwd<\/code><\/li>\n<li id=\"0460\" name=\"0460\"><code><strong>os.listdir()<\/strong><\/code> &mdash; get the contents of the current working directory as a list of strings &mdash; <code>ls<\/code><\/li>\n<li id=\"6e0d\" name=\"6e0d\"><code><strong>os.walk(&quot;starting_directory_path&quot;)<\/strong><\/code>&mdash; returns a generator with name and path info for directories and files in the the current directory and all subdirectories &mdash; no exact short CLI equivalent, but&nbsp;<code>ls -R<\/code>&nbsp;provides subdirectory names and the names of files within subdirectories<\/li>\n<\/ul>\n<h4 id=\"391b\" name=\"391b\">Change Things<\/h4>\n<ul>\n<li id=\"61c7\" name=\"61c7\"><code><strong>os.chdir(&quot;\/absolute\/or\/relative\/path&quot;)<\/strong><\/code> &mdash; change the current working directory &mdash; <code>cd<\/code><\/li>\n<li id=\"9218\" name=\"9218\"><code><strong>os.path.join()<\/strong><\/code>&mdash;create a path for later use &mdash; no short CLI equivalent<\/li>\n<li id=\"d90a\" name=\"d90a\"><code><strong>os.makedirs(&quot;dir1\/dir2&quot;)<\/strong><\/code><strong> <\/strong>&mdash; make directory &mdash;<code>mkdir-p<\/code><\/li>\n<li id=\"7a3b\" name=\"7a3b\"><code><strong>shutil.copy2(&quot;source_file_path&quot;, &quot;destination_directory_path&quot;)<\/strong><\/code> &mdash; copy a file or directory &mdash; <code>cp<\/code><\/li>\n<li id=\"5480\" name=\"5480\"><code><strong>shutil.move(&quot;source_file_path<em>&quot;<\/em>, &quot;destination_directory_path&quot;)<\/strong><\/code><strong> <\/strong>&mdash; move a file or directory &mdash; <code>mv<\/code><\/li>\n<li id=\"a67d\" name=\"a67d\"><code><strong>os.remove(&quot;my_file_path&quot;)<\/strong><\/code> &mdash; remove a file &mdash; <code>rm<\/code><\/li>\n<li id=\"c748\" name=\"c748\"><code><strong>shutil.rmtree(&quot;my_directory_path&quot;)<\/strong><\/code>&mdash; remove a directory and all files and directories in it &mdash;<code>rm -rf<\/code><\/li>\n<\/ul>\n<h3 id=\"9506\" name=\"9506\">Wrap<\/h3>\n<p id=\"92d3\" name=\"92d3\">Now you&rsquo;ve seen the basics of interacting with the file system in Python. Try these commands in your IPython interpreter for quick feedback. Then explain them to someone else to solidify your knowledge. You&rsquo;ll be less sore than if you had moved boxes of notebooks around your house. But the exercise would have been good, so now you can hit the gym instead.&nbsp;<\/p>\n<p id=\"0306\" name=\"0306\">If you want to go deeper, check out the free ebook&nbsp;<a data-href=\"https:\/\/automatetheboringstuff.com\/chapter9\/\" href=\"https:\/\/automatetheboringstuff.com\/chapter9\/\" rel=\"noopener noreferrer\" target=\"_blank\">Automate the Boring Stuff with Python<\/a>.<\/p>\n<p id=\"b98f\" name=\"b98f\">If you want to learn about reading and writing from files with Python check out the&nbsp;<a data-href=\"https:\/\/docs.python.org\/3\/library\/functions.html#open\" href=\"https:\/\/docs.python.org\/3\/library\/functions.html#open\" rel=\"noopener noreferrer\" target=\"_blank\"><em>open<\/em>&nbsp;function<\/a>. Remember to use a context manager like so:<code>&nbsp;with open(&lsquo;myfile&rsquo;) as file:<\/code>&nbsp;<\/p>\n<p id=\"fc3f\" name=\"fc3f\">I hope you found this intro to Python file system manipulation useful. If you did, please share it on your favorite social media channels so others can find it too.<\/p>\n<p id=\"e16d\" name=\"e16d\">I write about Python, Docker, data science, and more. If any of that&rsquo;s of interest to you, read more&nbsp;<a href=\"https:\/\/medium.com\/@jeffhale\" target=\"_blank\" rel=\"noopener noreferrer\" class=\"broken_link\">here<\/a>&nbsp;and follow me on Medium.<\/p>\n<\/section>\n","protected":false},"excerpt":{"rendered":"<p>In this guide, we&rsquo;ll look at methods from the&nbsp;os&nbsp;and&nbsp;shutil&nbsp;modules. The&nbsp;os module is the primary Python module for interacting with the operating system. The&nbsp;shutil module also contains high-level file operations. For some reason, you make directories with&nbsp;os&nbsp;but move and copy them with&nbsp;shutil. There are many ways to copy files and directories in Python.&nbsp;Go figure.<\/p>\n","protected":false},"author":369,"featured_media":3975,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"content-type":"","footnotes":""},"categories":[187],"tags":[94],"ppma_author":[2134],"class_list":["post-1540","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-bigdata-cloud","tag-data-science"],"authors":[{"term_id":2134,"user_id":369,"is_guest":0,"slug":"jeff-hale","display_name":"Jeff Hale","avatar_url":"https:\/\/secure.gravatar.com\/avatar\/?s=96&d=mm&r=g","user_url":"","last_name":"Hale","first_name":"Jeff","job_title":"","description":"Jeff Hale is a co-founder of Rebel Desk, where he oversees technology, finance, and operations for this company. He&nbsp;is an experienced entrepreneur who has managed technology, operations, and finances for several companies.&nbsp;"}],"_links":{"self":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/1540","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/users\/369"}],"replies":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/comments?post=1540"}],"version-history":[{"count":2,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/1540\/revisions"}],"predecessor-version":[{"id":31493,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/posts\/1540\/revisions\/31493"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media\/3975"}],"wp:attachment":[{"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/media?parent=1540"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/categories?post=1540"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/tags?post=1540"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.experfy.com\/blog\/wp-json\/wp\/v2\/ppma_author?post=1540"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}