{"id":21871,"date":"2022-12-22T06:15:00","date_gmt":"2022-12-22T05:15:00","guid":{"rendered":"https:\/\/expert-only.com\/?p=21871"},"modified":"2023-01-18T17:56:02","modified_gmt":"2023-01-18T16:56:02","slug":"manage-files-in-python","status":"publish","type":"post","link":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/","title":{"rendered":"Manage files in Python at file system level"},"content":{"rendered":"\n<p>How to manage files in Python like the create, delete, rename, or move operations? Python programming is a versatile language that can be used for a wide range of tasks. File management is the process of organizing, storing, and manipulating files on a computer or any remote file system. Using Python methods, you can easily <strong>automate repetitive file management tasks<\/strong>, such as creating, renaming, deleting, moving, and searching for files.<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_82_2 ez-toc-wrap-center counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<div class=\"ez-toc-title-container\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Table of Contents<\/p>\n<span class=\"ez-toc-title-toggle\"><\/span><\/div>\n<nav><ul class='ez-toc-list ez-toc-list-level-1 ' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#1-list-files-in-python\" >1. List files in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#2-read-file-attributes-in-python\" >2. Read file attributes in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#3-list-files-recursively-in-python\" >3. List files recursively in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#4-create-files-with-python\" >4. Create files with Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#5-create-temporary-files-in-python\" >5. Create temporary files in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#6-rename-files-in-python\" >6. Rename files in Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-7\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#7-delete-files\" >7. Delete files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-8\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#8-copy-files\" >8. Copy files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-9\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#9-move-files\" >9. Move files<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-10\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#10-search-files-with-python\" >10. Search files with Python<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-11\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#conclusion-on-file-management-in-python\" >Conclusion on file management in Python<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\" id=\"h-1-list-files-in-python\"><span class=\"ez-toc-section\" id=\"1-list-files-in-python\"><\/span>1. List files in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To list all the files in a directory in Python, you can use the <code>os<\/code> library and its <code>listdir()<\/code> function. The <code>listdir()<\/code> function takes the directory path as an argument and returns a list of all the files in that directory. A demonstration of the simplicity in utilizing this function to retrieve a list of all the files in the current directory is illustrated below.<\/p>\n\n\n\n<p>Let&#8217;s consider the <a href=\"https:\/\/expert-only.com\/en\/windows\/how-to-enable-dark-mode-in-windows-10\/\">Windows<\/a> folder below, it contains 6 files, including csv and txt files.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"680\" height=\"480\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-windows-directory.jpg\" alt=\"Manage files in Python to get the list of files inside a folder.\" class=\"wp-image-21911\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-windows-directory.jpg 680w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-windows-directory-300x212.jpg 300w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-windows-directory-678x480.jpg 678w\" sizes=\"auto, (max-width: 680px) 100vw, 680px\" \/><figcaption class=\"wp-element-caption\"><strong>Manage files in Python to get the list of files inside a folder.<\/strong><\/figcaption><\/figure><\/div>\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nfiles = os.listdir('C:\\data')\nfor file in files:\n    print(file)\n<\/pre>\n\n\n\n<p>The result of the script is the display of the list of 6 file names.<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"520\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-directory-listdir.jpg\" alt=\"Get and print the list of files inside the Windows folder to manage them in Python.\" class=\"wp-image-21916\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-directory-listdir.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-directory-listdir-300x217.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><strong>Get and print the list of files inside the Windows folder to manage them in Python.<\/strong><\/figcaption><\/figure><\/div>\n\n\n<p>Indeed, whether it is a Windows or Linux <a href=\"https:\/\/opensource.com\/life\/16\/10\/introduction-linux-filesystems\" target=\"_blank\" rel=\"noreferrer noopener\">file system<\/a>, one of the biggest strengths of Python programming is its ability to easily manage files in scripts when it comes to managing and analysing data.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"2-read-file-attributes-in-python\"><\/span>2. Read file attributes in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To read file attributes in Python, you can use the <code>os<\/code> library and its <code>stat()<\/code> function. The <code>stat()<\/code> function takes the file path as an argument and returns an object containing the file&#8217;s attributes such as creation time, modification time, size, etc.<\/p>\n\n\n\n<p>Here, is this second script example, check how to use this function to read the creation date and of all the text files within a folder.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nimport time\n\npath = \"C:\\data\"\nfiles = os.listdir(path)\nfor file in files:\n\n    file_path = os.path.join(path, file)\n    file_stats = os.stat(file_path)\n    create_time = time.ctime(file_stats.st_ctime)\n\n    print(file)\n    print(f'File creation time: {create_time}')\n<\/pre>\n\n\n\n<p>We use the same folder as the previous example, i.e., <strong>C:\\data<\/strong>. We also use <code>os.path.join()<\/code> to <strong>combine the folder path and the file name<\/strong>. Also, you need to use <code>os.stat(file_path)<\/code> to get the statistics of the text file. Also, the folder path should be enclosed in double quotes (&#8220;).<\/p>\n\n\n<div class=\"wp-block-image\">\n<figure class=\"aligncenter size-full\"><img loading=\"lazy\" decoding=\"async\" width=\"720\" height=\"640\" src=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-read-file-creation-date.jpg\" alt=\"Get the creation date of all the files in the directory using Python and print the list. \" class=\"wp-image-21921\" srcset=\"https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-read-file-creation-date.jpg 720w, https:\/\/expert-only.com\/wp-content\/uploads\/2023\/01\/python-manage-files-list-read-file-creation-date-300x267.jpg 300w\" sizes=\"auto, (max-width: 720px) 100vw, 720px\" \/><figcaption class=\"wp-element-caption\"><strong>Get the creation date of all the files in the directory using Python and print the list. <\/strong><\/figcaption><\/figure><\/div>\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"3-list-files-recursively-in-python\"><\/span>3. List files recursively in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To list all the files in a directory and its subdirectories recursively in Python, you can use the <code>os<\/code> library and its <code>walk()<\/code> function. The <code>walk()<\/code> function takes the directory path as an argument and generates the file names in a directory tree by walking the tree either top-down or bottom-up.<\/p>\n\n\n\n<p>This script uses the <strong><em>walk function<\/em><\/strong> to recursively list all the files in a directory and its subdirectories.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nfor root, dirs, files in os.walk(\"path\/to\/directory\"):\n    for file in files:\n        print(os.path.join(root, file))\n<\/pre>\n\n\n\n<p>To <a href=\"https:\/\/expert-only.com\/en\/ms-dos\/list-files-recursively-with-cmd\/\">list all files using the windows command line<\/a>, use the dir command and its recursive options.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"QxyEYBgPPa\"><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/list-files-recursively-with-cmd\/\">How to list Windows files recursively with cmd including subfolders?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to list Windows files recursively with cmd including subfolders?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/ms-dos\/list-files-recursively-with-cmd\/embed\/#?secret=d0TecTF3ou#?secret=QxyEYBgPPa\" data-secret=\"QxyEYBgPPa\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"4-create-files-with-python\"><\/span>4. Create files with Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>One of the most useful features when you manage files in Python is of course the file creation. To create a new file in Python, you can use the <code>open()<\/code> function. This function takes the file name and the mode as arguments. Here&#8217;s an example of how you can use this function to create a new file. <\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">with open(\"example.txt\", \"w\") as file:\n    file.write(\"Example content\")\n<\/pre>\n\n\n\n<p>By the way, the open method is well explained with much more details in this <a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">tutorial about text files management in Python<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"SZuOSTi7Sv\"><a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">Manage text files in Python using read and write methods<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Manage text files in Python using read and write methods&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/embed\/#?secret=MF5vspSg4I#?secret=SZuOSTi7Sv\" data-secret=\"SZuOSTi7Sv\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"5-create-temporary-files-in-python\"><\/span>5. Create temporary files in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>To create a temporary file in Python, you can use the <code>tempfile<\/code> library and its <code>NamedTemporaryFile()<\/code> function. This function creates a temporary file with a unique name and returns a file object. Here&#8217;s one more script example, this time to use the <strong>tempfile function<\/strong> to create a temporary file.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import tempfile\ntemp = tempfile.NamedTemporaryFile(delete=False)\nprint(temp.name)\n\n# result: C:\\Users\\user1\\AppData\\Local\\Temp\\tmp7chtz52e\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"6-rename-files-in-python\"><\/span>6. Rename files in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Renaming files in Python can be done using the <code>os<\/code> library and its <code>rename()<\/code> function. This function takes the current file name and the new file name as arguments. Use the following script to easily rename a file, in this case the file does not have any extension.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nos.rename(\"path\/to\/current\/file\", \"path\/to\/new\/file\")\n<\/pre>\n\n\n\n<p>Another way to rename a file is by using the <code>shutil<\/code> library and its <code>move()<\/code> function. This function also takes the current file name and the new file name as arguments.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import shutil\n\nshutil.move(\"path\/to\/current\/file\", \"path\/to\/new\/file\")\n<\/pre>\n\n\n\n<p>Renaming files with Python scripts can be a powerful way to organize and keep your files in order. It&#8217;s a simple task but it can save you a lot of time in the long run. With just a few lines of code, you can <strong>easily rename multiple files at once<\/strong>, making it a breeze to keep your files organized and easy to find.<\/p>\n\n\n\n<p>Whether you&#8217;re working on a personal project or running a business, renaming files is a must-have skill for anyone working with a large amount of data stored in numerous files.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"7-delete-files\"><\/span>7. Delete files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Deleting files in Python can be done using the <code>os<\/code> library and its <code>remove()<\/code> function. This function takes the file name as an argument.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nos.remove(\"path\/to\/file\")\n<\/pre>\n\n\n\n<p>Another way to delete a file is by using the <code>shutil<\/code> library and its <code>rmtree()<\/code> function. This function can be used to delete a directory and all its contents. Python deletion operations can be used to remove unnecessary files, like old versions of files, or files that are taking up too much space.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import shutil\n\nshutil.rmtree(\"path\/to\/directory\")\n<\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"8-copy-files\"><\/span>8. Copy files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Copying files in Python can be done using the <code>shutil<\/code> library and its <code>copy()<\/code> function. This function takes the source file name and the destination file name as arguments. Here&#8217;s an example of to use this function in a script to copy a file:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import shutil\n\nshutil.copy(\"path\/to\/source\/file\", \"path\/to\/destination\/file\")\n<\/pre>\n\n\n\n<p>Another way to copy a file is by using the <code>os<\/code> library and its <code>system()<\/code> function. This function can be used to run command line commands.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nos.system(\"cp path\/to\/source\/file path\/to\/destination\/file\")\n<\/pre>\n\n\n\n<p>Copying files is a crucial task that not only keeps your data safe but also enables you to easily share it across different systems. Using Python methods, you can quickly copy multiple files at once, making it a breeze to keep your files organized and easily accessible.<\/p>\n\n\n\n<p>Indeed, the ability to manage files in Python is an important skill for anyone working with file systems. So go ahead and master the art of copying files with Python and take control of your data.<\/p>\n\n\n\n<p>Another very common way to copy many files at once, is to use <strong>MS scripting tools<\/strong>, like MS-DOS or PowerShell. This other tutorial explains <a href=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-files-recursively-with-cmd\/\">how to copy files and folders recursively<\/a>. <\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"Ylt7Xe8T2k\"><a href=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-files-recursively-with-cmd\/\">How to copy files recursively using Windows cmd?<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;How to copy files recursively using Windows cmd?&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/ms-dos\/copy-files-recursively-with-cmd\/embed\/#?secret=63mFPWSsBN#?secret=Ylt7Xe8T2k\" data-secret=\"Ylt7Xe8T2k\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"9-move-files\"><\/span>9. Move files<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Moving files in Python can be done using the <code>shutil<\/code> library and its <code>move()<\/code> function. This function takes the source file name and the destination file name as arguments. An example of using the function to move a file can be seen here:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import shutil\nshutil.move(\"path\/to\/source\/file\", \"path\/to\/destination\/file\")\n<\/pre>\n\n\n\n<p>Another way to move a file is by using the <code>os<\/code> library and its <code>rename()<\/code> function. This function also takes the source file name and the destination file name as arguments.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\nos.rename(\"path\/to\/source\/file\", \"path\/to\/destination\/file\")\n<\/pre>\n\n\n\n<p>Moving files is an essential task in <strong>Python file management<\/strong>. It allows you to <strong>reorganize<\/strong> files, <strong>move<\/strong> them to different directories, or even <strong>rename<\/strong> them.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"10-search-files-with-python\"><\/span>10. Search files with Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>Searching for files in Python can be done using the <code>glob<\/code> library and its <code>glob()<\/code> function. This function takes a file pattern as an argument and returns a list of file names that match the pattern.<\/p>\n\n\n\n<p>Use for example the function below to search for all the files having a <strong><em>.txt<\/em><\/strong> extension in the directory passed as parameter:<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import glob\n\ntxt_files = glob.glob(\"path\/to\/directory\/*.txt\")\nprint(txt_files)\n<\/pre>\n\n\n\n<p>Another way to search for files is by using the <code>os<\/code> library and its <code>scandir()<\/code> function. This function returns an iterator that yields the files and directories in the specified directory.<\/p>\n\n\n\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"python\" data-enlighter-theme=\"\" data-enlighter-highlight=\"\" data-enlighter-linenumbers=\"\" data-enlighter-lineoffset=\"\" data-enlighter-title=\"\" data-enlighter-group=\"\">import os\n\nfor file in os.scandir(\"path\/to\/directory\"):\n    if file.name.endswith(\".txt\"):\n        print(file.name)\n<\/pre>\n\n\n\n<p>Searching for files is an important aspect of file management, it enables you to quickly locate the files you require, at the time you need them. With a minimal amount of code, you can search for multiple files at once, based on various criteria.<\/p>\n\n\n\n<p>For both all IT projects and business operations, the capability to search for files is useful for those working with data. Empower yourself and master the art of managing files using Python.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"conclusion-on-file-management-in-python\"><\/span>Conclusion on file management in Python<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>In conclusion, using Python for managing files is a powerful tool for anyone working with data. Whether you need to list, read, create, rename, delete, copy, move or search for files, Python offers various libraries and functions that make it easy to accomplish these tasks.<\/p>\n\n\n\n<p>From <strong>listing all the files in a directory recursively<\/strong> and also in its subdirectories, to creating temporary files, <strong>renaming multiple files at once<\/strong>, or searching for files based on different criteria, Python offers a wide range of possibilities for file management. With simple Python scripts, accomplish all these automated tasks and keep files organized and easily accessible.<\/p>\n\n\n\n<p>Once the file management at OS level is acquired, you can start using the file management to <a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">read and write data from and into text  or csv files<\/a>.<\/p>\n\n\n\n<figure class=\"wp-block-embed is-type-wp-embed is-provider-sql-and-it-tutorials wp-block-embed-sql-and-it-tutorials\"><div class=\"wp-block-embed__wrapper\">\n<blockquote class=\"wp-embedded-content\" data-secret=\"SZuOSTi7Sv\"><a href=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/\">Manage text files in Python using read and write methods<\/a><\/blockquote><iframe loading=\"lazy\" class=\"wp-embedded-content\" sandbox=\"allow-scripts\" security=\"restricted\" style=\"position: absolute; clip: rect(1px, 1px, 1px, 1px);\" title=\"&#8220;Manage text files in Python using read and write methods&#8221; &#8212; SQL and IT Tutorials\" src=\"https:\/\/expert-only.com\/en\/python\/text-files-in-python\/embed\/#?secret=MF5vspSg4I#?secret=SZuOSTi7Sv\" data-secret=\"SZuOSTi7Sv\" width=\"500\" height=\"282\" frameborder=\"0\" marginwidth=\"0\" marginheight=\"0\" scrolling=\"no\"><\/iframe>\n<\/div><\/figure>\n","protected":false},"excerpt":{"rendered":"<div class=\"mh-excerpt\"><p>How to manage files in Python like the create, delete, rename, or move operations? Python programming is a versatile language that can be used for a wide range of tasks. File management is the process of organizing, storing, and <a class=\"mh-excerpt-more\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\" title=\"Manage files in Python at file system level\">&#8230;<\/a><\/p>\n<\/div>","protected":false},"author":1,"featured_media":10509,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[657],"tags":[],"class_list":{"0":"post-21871","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"has-post-thumbnail","7":"category-python"},"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v21.7 (Yoast SEO v26.2) - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Manage files in Python - File management - Python tutorials<\/title>\n<meta name=\"description\" content=\"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.\" \/>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Manage files in Python at file system level\" \/>\n<meta property=\"og:description\" content=\"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.\" \/>\n<meta property=\"og:url\" content=\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\" \/>\n<meta property=\"og:site_name\" content=\"SQL and IT Tutorials\" \/>\n<meta property=\"article:publisher\" content=\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\" \/>\n<meta property=\"article:published_time\" content=\"2022-12-22T05:15:00+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2023-01-18T16:56:02+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"1920\" \/>\n\t<meta property=\"og:image:height\" content=\"1080\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Expert-Only\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:creator\" content=\"@expert_only\" \/>\n<meta name=\"twitter:site\" content=\"@expert_only\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Expert-Only\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"9 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\"},\"author\":{\"name\":\"Expert-Only\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\"},\"headline\":\"Manage files in Python at file system level\",\"datePublished\":\"2022-12-22T05:15:00+00:00\",\"dateModified\":\"2023-01-18T16:56:02+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\"},\"wordCount\":1451,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg\",\"articleSection\":[\"Python tutorials\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\",\"url\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\",\"name\":\"Manage files in Python - File management - Python tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/expert-only.com\/en\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg\",\"datePublished\":\"2022-12-22T05:15:00+00:00\",\"dateModified\":\"2023-01-18T16:56:02+00:00\",\"description\":\"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.\",\"breadcrumb\":{\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg\",\"width\":1920,\"height\":1080},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"en\",\"item\":\"https:\/\/expert-only.com\/en\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Manage files in Python at file system level\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/expert-only.com\/en\/#website\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"name\":\"SQL and IT Tutorials\",\"description\":\"\",\"publisher\":{\"@id\":\"https:\/\/expert-only.com\/en\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/expert-only.com\/en\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/expert-only.com\/en\/#organization\",\"name\":\"Expert-Only\",\"url\":\"https:\/\/expert-only.com\/en\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"contentUrl\":\"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg\",\"width\":381,\"height\":174,\"caption\":\"Expert-Only\"},\"image\":{\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/\"},\"sameAs\":[\"https:\/\/www.facebook.com\/ExpertOnlyCom\/\",\"https:\/\/x.com\/expert_only\",\"https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA\"]},{\"@type\":\"Person\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef\",\"name\":\"Expert-Only\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g\",\"caption\":\"Expert-Only\"}}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"Manage files in Python - File management - Python tutorials","description":"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.","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:\/\/expert-only.com\/en\/python\/manage-files-in-python\/","og_locale":"en_US","og_type":"article","og_title":"Manage files in Python at file system level","og_description":"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.","og_url":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/","og_site_name":"SQL and IT Tutorials","article_publisher":"https:\/\/www.facebook.com\/ExpertOnlyCom\/","article_published_time":"2022-12-22T05:15:00+00:00","article_modified_time":"2023-01-18T16:56:02+00:00","og_image":[{"width":1920,"height":1080,"url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg","type":"image\/jpeg"}],"author":"Expert-Only","twitter_card":"summary_large_image","twitter_creator":"@expert_only","twitter_site":"@expert_only","twitter_misc":{"Written by":"Expert-Only","Est. reading time":"9 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#article","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/"},"author":{"name":"Expert-Only","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef"},"headline":"Manage files in Python at file system level","datePublished":"2022-12-22T05:15:00+00:00","dateModified":"2023-01-18T16:56:02+00:00","mainEntityOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/"},"wordCount":1451,"commentCount":0,"publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg","articleSection":["Python tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/","url":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/","name":"Manage files in Python - File management - Python tutorials","isPartOf":{"@id":"https:\/\/expert-only.com\/en\/#website"},"primaryImageOfPage":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage"},"image":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage"},"thumbnailUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg","datePublished":"2022-12-22T05:15:00+00:00","dateModified":"2023-01-18T16:56:02+00:00","description":"Manage files in Python with methods for creating, renaming, deleting, moving, and searching for files and automate file management tasks.","breadcrumb":{"@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#primaryimage","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2022\/09\/coffee-631768_1920x1080.jpg","width":1920,"height":1080},{"@type":"BreadcrumbList","@id":"https:\/\/expert-only.com\/en\/python\/manage-files-in-python\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"en","item":"https:\/\/expert-only.com\/en\/"},{"@type":"ListItem","position":2,"name":"Manage files in Python at file system level"}]},{"@type":"WebSite","@id":"https:\/\/expert-only.com\/en\/#website","url":"https:\/\/expert-only.com\/en\/","name":"SQL and IT Tutorials","description":"","publisher":{"@id":"https:\/\/expert-only.com\/en\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/expert-only.com\/en\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/expert-only.com\/en\/#organization","name":"Expert-Only","url":"https:\/\/expert-only.com\/en\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/","url":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","contentUrl":"https:\/\/expert-only.com\/wp-content\/uploads\/2017\/09\/cropped-logo_Expert-Only.jpg","width":381,"height":174,"caption":"Expert-Only"},"image":{"@id":"https:\/\/expert-only.com\/en\/#\/schema\/logo\/image\/"},"sameAs":["https:\/\/www.facebook.com\/ExpertOnlyCom\/","https:\/\/x.com\/expert_only","https:\/\/www.youtube.com\/channel\/UCMS5sR_FwAetB0FmciNvUaA"]},{"@type":"Person","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/406a9576b52944f018739a42046873ef","name":"Expert-Only","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/expert-only.com\/en\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/084b15660763ff5b13bb60b2f52f97bb?s=96&d=identicon&r=g","caption":"Expert-Only"}}]}},"_links":{"self":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21871","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/comments?post=21871"}],"version-history":[{"count":0,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/posts\/21871\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media\/10509"}],"wp:attachment":[{"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/media?parent=21871"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/categories?post=21871"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/expert-only.com\/en\/wp-json\/wp\/v2\/tags?post=21871"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}