

{"id":19482,"date":"2018-06-24T04:05:52","date_gmt":"2018-06-23T22:35:52","guid":{"rendered":"https:\/\/data-flair.training\/blogs\/?p=19482"},"modified":"2026-04-20T16:12:17","modified_gmt":"2026-04-20T10:42:17","slug":"python-rename-file","status":"publish","type":"post","link":"https:\/\/data-flair.training\/blogs\/python-rename-file\/","title":{"rendered":"How Python Rename File &#8211; Single &amp; Multiple Files With Example"},"content":{"rendered":"<p>Renaming files is a common task while organizing data, managing projects, or automating workflows.<\/p>\n<p>We will also explore how to handle multiple files, apply patterns while renaming, and avoid common errors like missing files or incorrect paths.<\/p>\n<p>For renaming files in Python, we will use the method rename() from the module os.<\/p>\n<p>Let\u2019s begin with how to rename a file in Python.<\/p>\n<h3>What is os.rename() in Python?<\/h3>\n<p>A messy naming scheme can break code. Python fixes it with os.rename(&#8220;old.txt&#8221;, &#8220;new.txt&#8221;). One call swaps names without touching content, making \u201crename files in Python\u201d a common search for developers tidying folders.<\/p>\n<p>As the name suggests, it lets us rename files and directories<\/p>\n<h3 class=\"western\">Syntax of os.rename() in Python<\/h3>\n<p>We have the following syntax for the rename() method:<\/p>\n<pre class=\"EnlighterJSRAW\">os.rename(src, dst)<\/pre>\n<p>Here, <i>src<\/i> is the source file or directory. <i>dst<\/i> is the destination file or directory. It doesn\u2019t return any value. Let\u2019s take an example.<\/p>\n<h3 class=\"western\">os.rename() Example in Python<\/h3>\n<p>In the following example, we rename the folder \u2018NewFolder\u2019 to \u2018Photos\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os,sys\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop')\r\n&gt;&gt;&gt; print(f\"We have: {os.listdir()}\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">We have<br \/>\n[&#8216;0.jpg&#8217;, &#8216;1.txt&#8217;, \u2018Documents\u2019, &#8216;NewFolder&#8217;, &#8216;Today.txt&#8217;]<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.rename('NewFolder',\u2019Photos\u2019) #This does the renaming\r\n&gt;&gt;&gt; os.listdir()<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">[&#8216;0.jpg&#8217;, &#8216;1.txt&#8217;, \u2018Documents\u2019, \u2018Photos\u2019, &#8216;Today.txt&#8217;]<\/div>\n<p>As you can see, this has renamed the directory \u2018NewFolder\u2019 to \u2018Photos\u2019.<\/p>\n<p>At the time of writing this article, we tried to rename the directory to the name \u2018Documents\u2019.<\/p>\n<p>This raised the following error:<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; os.rename('NewFolder','Documents')<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">\n<p>Traceback (most recent call last):File &#8220;&lt;pyshell#7&gt;&#8221;, line 1, in &lt;module&gt;<\/p>\n<p>os.rename(&#8216;NewFolder&#8217;,&#8217;Documents&#8217;)<\/p>\n<p>FileExistsError: [WinError 183] Cannot create a file when that file already exists: &#8216;NewFolder&#8217; -&gt; &#8216;Documents&#8217;<\/p>\n<\/div>\n<h3 class=\"western\">How to rename multiple files in Python?<\/h3>\n<p>Suppose in this folder \u2018Photos\u2019, we have the following photos of dogs.<\/p>\n<p>Here, we can manually rename each of these files, but what if we had over a hundred photos? Or maybe thousands? It would be too much to do.<\/p>\n<p>Instead, we make Python do it. Watch how.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; import os\r\n&gt;&gt;&gt; os.chdir('C:\\\\Users\\\\lifei\\\\Desktop\\\\Photos')\r\n&gt;&gt;&gt; i=1\r\n&gt;&gt;&gt; for file in os.listdir():\r\n      src=file\r\n      dst=\"Dog\"+str(i)+\".jpg\"\r\n      os.rename(src,dst)\r\n      i+=1<\/pre>\n<h3 class=\"western\">Rename File (Single File) in Python<\/h3>\n<p>Sometimes, we may want to rename just one file from tens or hundreds. We can search for it and then use Python to rename it.<\/p>\n<p>In the following code, we rename \u2018Dog7.jpg\u2019 to \u2018SeventhDog.jpg\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; for file in os.listdir():\r\n    src=file\r\n    if src=='Dog7.jpg':\r\n           dst=\"Seventh Dog.jpg\"\r\n           os.rename(src,dst)<\/pre>\n<h3 class=\"western\">An Example- Incrementing Each Picture Number<\/h3>\n<p>In this example, we increment the picture number for each picture in this folder.<\/p>\n<p>For this example, we have renamed \u2018Seventh Dog.jpg\u2019 to \u2018Dog7.jpg\u2019.<\/p>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; print(\"How many pictures?\")<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">How many pictures?<\/div>\n<pre class=\"EnlighterJSRAW\">&gt;&gt;&gt; i=int(input())<\/pre>\n<p><strong>Output<\/strong><\/p>\n<div class=\"code-output\">10<\/div>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"null\">&gt;&gt;&gt; count=1\r\n&gt;&gt;&gt; while(count&lt;=10): \r\n         for file in os.listdir(): \r\n             src=file\r\n                 if src=='Dog'+str(i)+'.jpg': \r\n                      dst='Dog'+str(i+1)+'.jpg' \r\n                      os.rename(src,dst) \r\n                      i-=1 \r\n                      count+=1<\/pre>\n<p>This code does the following:<\/p>\n<p>What we do here is that we ask the user how many pictures she has. We set a variable <i>count<\/i> to 1. We keep processing till <i>count<\/i> becomes equal to 10.<\/p>\n<p>For each time we are in the while loop, we check the filenames one by one. We compare the file names; the first question is- Is the filename \u2018Dog10.jpg\u2019?<\/p>\n<p>For the file, it renames it by incrementing the number. Then, we decrement <i>i<\/i> by 1 and increment <i>count<\/i> by 1.<\/p>\n<p>Hence, once the count has reached 11, we have fulfilled our purpose by now.<\/p>\n<p>So, this was all about the &#8221; How Python Renames Files tutorial. Hope you like our explanation.<\/p>\n<h3>Python Interview Questions on Rename File<\/h3>\n<p>1. How to rename a file in Python?<\/p>\n<p>2. How to change the name of an image in Python?<\/p>\n<p>3. How to check if a file exists in Python?<\/p>\n<p>4. How to rename multiple file using Python?<\/p>\n<p>5. How to write to a file in Python?<\/p>\n<h3 class=\"western\">Conclusion<\/h3>\n<p>Hence, we learned how to rename files (single and multiple files) with the os.rename module in Python.<\/p>\n<p>In addition, we discuss an example to understand this process better. Got more ideas? Drop them in the comments.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Renaming files is a common task while organizing data, managing projects, or automating workflows. We will also explore how to handle multiple files, apply patterns while renaming, and avoid common errors like missing files&#46;&#46;&#46;<\/p>\n","protected":false},"author":5,"featured_media":19505,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[46],"tags":[4312,5933,5934,9339,9340,9341,16492,10446,10810,10813,11524,11528,14057],"class_list":["post-19482","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python","tag-example-of-os-rename","tag-how-python-rename-file","tag-how-python-rename-file-in-bulk","tag-os-rename","tag-os-rename-example","tag-os-rename-syntax","tag-python-backup-files","tag-python-copy-and-rename-file","tag-python-rename-file","tag-python-rename-multiple-files","tag-rename-files-in-sequence-python","tag-renaming-files-in-python","tag-syntax-of-os-rename"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.8 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>How Python Rename File - Single &amp; Multiple Files With Example - DataFlair<\/title>\n<meta name=\"description\" content=\"How Python Rename File - Renaming Single file in Python, Python rename Multiple Files, OS. Module in Python, Example of Incrementing Each Picture Number\" \/>\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\/python-rename-file\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How Python Rename File - Single &amp; Multiple Files With Example - DataFlair\" \/>\n<meta property=\"og:description\" content=\"How Python Rename File - Renaming Single file in Python, Python rename Multiple Files, OS. Module in Python, Example of Incrementing Each Picture Number\" \/>\n<meta property=\"og:url\" content=\"https:\/\/data-flair.training\/blogs\/python-rename-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-23T22:35:52+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2026-04-20T10:42:17+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-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=\"3 minutes\" \/>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"How Python Rename File - Single &amp; Multiple Files With Example - DataFlair","description":"How Python Rename File - Renaming Single file in Python, Python rename Multiple Files, OS. Module in Python, Example of Incrementing Each Picture Number","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\/python-rename-file\/","og_locale":"en_US","og_type":"article","og_title":"How Python Rename File - Single &amp; Multiple Files With Example - DataFlair","og_description":"How Python Rename File - Renaming Single file in Python, Python rename Multiple Files, OS. Module in Python, Example of Incrementing Each Picture Number","og_url":"https:\/\/data-flair.training\/blogs\/python-rename-file\/","og_site_name":"DataFlair","article_publisher":"https:\/\/www.facebook.com\/DataFlairWS\/","article_published_time":"2018-06-23T22:35:52+00:00","article_modified_time":"2026-04-20T10:42:17+00:00","og_image":[{"width":1200,"height":628,"url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-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":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#article","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/"},"author":{"name":"DataFlair Team","@id":"https:\/\/data-flair.training\/blogs\/#\/schema\/person\/7f83c342f5d1632d6f7b4b0b0f447823"},"headline":"How Python Rename File &#8211; Single &amp; Multiple Files With Example","datePublished":"2018-06-23T22:35:52+00:00","dateModified":"2026-04-20T10:42:17+00:00","mainEntityOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/"},"wordCount":599,"commentCount":0,"publisher":{"@id":"https:\/\/data-flair.training\/blogs\/#organization"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-File-01.jpg","keywords":["Example of os.rename()","How Python Rename File","How Python Rename File in Bulk","os.rename()","os.rename() Example","os.rename() Syntax","Python Backup Files","python copy and rename file","Python Rename File","python rename multiple files","rename files in sequence python","renaming files in python","Syntax of os.rename()"],"articleSection":["Python Tutorials"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/data-flair.training\/blogs\/python-rename-file\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/","url":"https:\/\/data-flair.training\/blogs\/python-rename-file\/","name":"How Python Rename File - Single &amp; Multiple Files With Example - DataFlair","isPartOf":{"@id":"https:\/\/data-flair.training\/blogs\/#website"},"primaryImageOfPage":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#primaryimage"},"image":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#primaryimage"},"thumbnailUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-File-01.jpg","datePublished":"2018-06-23T22:35:52+00:00","dateModified":"2026-04-20T10:42:17+00:00","description":"How Python Rename File - Renaming Single file in Python, Python rename Multiple Files, OS. Module in Python, Example of Incrementing Each Picture Number","breadcrumb":{"@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/data-flair.training\/blogs\/python-rename-file\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/data-flair.training\/blogs\/python-rename-file\/#primaryimage","url":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-File-01.jpg","contentUrl":"https:\/\/data-flair.training\/blogs\/wp-content\/uploads\/sites\/2\/2018\/06\/Python-Rename-File-01.jpg","width":1200,"height":628,"caption":"How Python Rename File - Single &amp; Multiple Files With Example"},{"@type":"BreadcrumbList","@id":"https:\/\/data-flair.training\/blogs\/python-rename-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 Rename File &#8211; Single &amp; Multiple Files With Example"}]},{"@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\/19482","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=19482"}],"version-history":[{"count":16,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/19482\/revisions"}],"predecessor-version":[{"id":147721,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/posts\/19482\/revisions\/147721"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media\/19505"}],"wp:attachment":[{"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/media?parent=19482"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/categories?post=19482"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/data-flair.training\/blogs\/wp-json\/wp\/v2\/tags?post=19482"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}