{"id":8004,"date":"2021-01-26T17:55:57","date_gmt":"2021-01-26T12:25:57","guid":{"rendered":"https:\/\/www.h2kinfosys.com\/blog\/?p=8004"},"modified":"2021-01-30T16:33:08","modified_gmt":"2021-01-30T11:03:08","slug":"python-rename-file-and-directory-using-os-rename","status":"publish","type":"post","link":"https:\/\/www.h2kinfosys.com\/blog\/python-rename-file-and-directory-using-os-rename\/","title":{"rendered":"Python Rename File and Directory using os.rename()"},"content":{"rendered":"\n<p>When working with servers you mostly get a terminal to perform different functions. Sometimes you need to write a different script that requires you to rename a file or directory. Let\u2019s take a look at how to rename a file and directory using Python \u201cos\u201d library.<\/p>\n\n\n\n<p>There is a <a href=\"https:\/\/www.h2kinfosys.com\/blog\/what-are-python-functions\/\" class=\"rank-math-link\">python built-in function<\/a> os.rename(src, dest) that take two arguments source location of the file or directory and destination location of the file or directory.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Renaming File<\/h2>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/-XBMFO-ZCLfyQ0AQ9NAUdacd1TRC7mF3waS3Sn0dv1ppVjXOufNwkMlgmrwlklgxGPqjERz6lotMH8Zf8TD508XT2UyAoljs5mIH5ZZHuF2donXzJIhAjzvCx45W6-spqaLF_hE\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s run the following code.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-pale-pink-background-color has-background\"><tbody><tr><td><code>import os<br><br>os.rename('rename-me.txt', 'i-am-file.txt')<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><a href=\"https:\/\/www.python.org\/\" rel=\"nofollow noopener\" target=\"_blank\"><img decoding=\"async\" src=\"https:\/\/lh4.googleusercontent.com\/YEtgnVsofDW-LIHSsnRVinTgVM2fA0ha7VV1paHopqkGA6YQLw-MJwG-_5EPNXFkW6017UKY5C10MqSum8LXoW-shPnhkO6F4sC1eN26ZqgNVHWFLlZh6tkh8SbWH3U8vIgwLKk\" alt=\"\" title=\"\"><\/a><\/figure>\n\n\n\n<h2 class=\"wp-block-heading\">Renaming Directory<\/h2>\n\n\n\n<p>Now let\u2019s rename the directory. Renaming directory is same as renaming file.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/0eWdFncS0zkzrjArinCUrmPhPitB_ylLXCXYPqzt4Mn5u7vojBRjX7wu-jKA4MohlXlXRRiQC5gaWgVPO4BWj4ZcjzGeYHHw75yYLYGX0YIM9kBW6vwHfmP5TbY_5DLqg3YOlY8\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p>Let\u2019s execute the following code to rename the directory.<\/p>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-subtle-pale-pink-background-color has-background\"><tbody><tr><td><code>import os<br><br>os.rename('rename-dir', 'dir-renamed')<\/code><\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/lh5.googleusercontent.com\/G5jJAFgBa3eoleDdAUXfOCG6xb90s3YDMT4HjY0n6lZ-BdHB0NJk7cqluQ4v3xIAEetFOrsWQ5qLlDTJG8YWlfT3loe1HeLRov8ut8tdXIQL6lLM1k3QB3afocFWQ_jwI7xxWsk\" alt=\"\" title=\"\"><\/figure>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When working with servers you mostly get a terminal to perform different functions. Sometimes you need to write a different script that requires you to rename a file or directory. Let\u2019s take a look at how to rename a file and directory using Python \u201cos\u201d library. There is a python built-in function os.rename(src, dest) that [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[342],"tags":[],"class_list":["post-8004","post","type-post","status-publish","format-standard","hentry","category-python-tutorials"],"_links":{"self":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8004","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/comments?post=8004"}],"version-history":[{"count":0,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/posts\/8004\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/media?parent=8004"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/categories?post=8004"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.h2kinfosys.com\/blog\/wp-json\/wp\/v2\/tags?post=8004"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}