{"id":4504,"date":"2023-12-14T18:34:12","date_gmt":"2023-12-14T18:34:12","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4504"},"modified":"2024-09-04T15:09:23","modified_gmt":"2024-09-04T14:09:23","slug":"working-with-files","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/file-input-and-output\/working-with-files","title":{"rendered":"Working With Files"},"content":{"rendered":"\n<p><em>We use pathlib <em>and forward slashes <\/em>so that the Path() function can be used to automatically convert backslash to forward slash on Linux systems.<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Does file exist?<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\n\nif not Path('C:\/MyDirectoryname\/MyFileName.txt').exists():\n   print(\"Does not exist\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Getting file name elements<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\n\n    my_path = Path(\"some_folder\/some_sub_folder\/my_filename.txt\")\n    \n    print(my_path.name)    #Prints \"my_filename.txt\"\n    print(my_path.suffix)  #Prints \"txt\n    print(my_path.stem)    #Prints \"my_filename\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Delete file<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\nif os.path.exists(\"myfile.txt\"):\n  os.remove(\"myfile.txt\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>We use pathlib and forward slashes so that the Path() function can be used to automatically convert backslash to forward slash on Linux systems. Does file exist? Getting file name elements Delete file<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[344],"tags":[],"class_list":["post-4504","post","type-post","status-publish","format-standard","hentry","category-file-input-and-output"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4504","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/comments?post=4504"}],"version-history":[{"count":9,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4504\/revisions"}],"predecessor-version":[{"id":4669,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4504\/revisions\/4669"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4504"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4504"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4504"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}