{"id":4530,"date":"2010-10-16T08:21:57","date_gmt":"2010-10-16T12:21:57","guid":{"rendered":"http:\/\/www.schollnick.net\/wordpress\/?p=4530"},"modified":"2010-11-08T15:44:59","modified_gmt":"2010-11-08T19:44:59","slug":"renaming-2-thousand-files-do-it-the-python-way","status":"publish","type":"post","link":"http:\/\/www.schollnick.net\/wordpress\/2010\/10\/renaming-2-thousand-files-do-it-the-python-way\/","title":{"rendered":"Renaming 2 thousand Files? Do it the Python way&#8230;"},"content":{"rendered":"<p>I ran into a situation earlier that I needed to bulk rename the file extension on a couple thousand files&#8230;.<\/p>\n<p>The first hurtle was that a majority of the files had a &#8220;\\&#8221; \u00c2\u00a0(or was it &#8220;\/&#8221;?) in it. \u00c2\u00a0Since I am on a Unix platform, it appears that the os.listdir function wasn&#8217;t able to see those files, since I believe that&#8217;s a reserved character. \u00c2\u00a0These files were originally \u00c2\u00a0created on an OS\/2 based computer&#8230;<\/p>\n<p>So I ended up using\u00c2\u00a0<a href=\"http:\/\/web.mac.com\/mickeyroberson\/MRR_Software\/NameChanger.html\">NameChanger<\/a>, and it is a utility that works well&#8230; \u00c2\u00a0Especially since it&#8217;s free.. \u00c2\u00a0The major drawback that I saw with NameChanger is that it didn&#8217;t seem to refresh it&#8217;s list of files once they were renamed&#8230; \u00c2\u00a0And it clears the Find \/ Replace (Original Text \/ New Text) fields after running.<\/p>\n<p>But once that hurdle was cleared, I needed to add file extensions&#8230; \u00c2\u00a0So, what does any programmer do in a case like this?  Write an python application:<\/p>\n<pre>import os\r\nimport sys<\/pre>\n<pre>filenames = os.listdir (\".\")\r\nfor filename in filenames:\r\n    if filename.endswith (\"html.txt\"):\r\n        new_filename = filename.replace (\".txt\", \"\")\r\n        os.rename (filename, new_filename)<\/pre>\n<pre>    (shortname, extension) = os.path.splitext (filename)\r\n    if extension == \"\":\r\n        new_filename = filename +\".txt\"\r\n        os.rename (filename, new_filename)<\/pre>\n<p>Yes, this is a very single purpose application, but if someone finds it useful feel free to use this as a starting point.<\/p>\n<p>The application will gather a list of files that are in the current directory, anything that has an &#8220;.html.txt&#8221; extension will be renamed to .txt. \u00c2\u00a0If there is no file extension then an .txt extension will be added&#8230;<\/p>\n<p>Why did I make this? \u00c2\u00a0I have a directory that I use to gather usenet messages in, and quicklook wouldn&#8217;t work until I renamed them to .txt (or .jpg, etc). \u00c2\u00a0Since this directory was 99% text files, I needed a fast and dirty way to rename them, thus the script above.<\/p>\n<p>Feel free to make any suggestions or to propose better methods just leave them in the comments&#8230;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I ran into a situation earlier that I needed to bulk rename the file extension on a couple thousand files&#8230;. The first hurtle was that a majority of the files had a &#8220;\\&#8221; \u00c2\u00a0(or was it &#8220;\/&#8221;?) in it. \u00c2\u00a0Since I am on a Unix platform, it appears that the os.listdir function wasn&#8217;t able to <a class=\"read-more\" href=\"http:\/\/www.schollnick.net\/wordpress\/2010\/10\/renaming-2-thousand-files-do-it-the-python-way\/\">[&hellip;]<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0},"categories":[12,7],"tags":[],"_links":{"self":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/posts\/4530"}],"collection":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/comments?post=4530"}],"version-history":[{"count":0,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/posts\/4530\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/media?parent=4530"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/categories?post=4530"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.schollnick.net\/wordpress\/wp-json\/wp\/v2\/tags?post=4530"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}<!-- WP Super Cache is installed but broken. The constant WPCACHEHOME must be set in the file wp-config.php and point at the WP Super Cache plugin directory. -->