{"id":4559,"date":"2024-01-04T16:28:09","date_gmt":"2024-01-04T16:28:09","guid":{"rendered":"https:\/\/ibex.tech\/python\/?p=4559"},"modified":"2025-01-30T13:58:24","modified_gmt":"2025-01-30T13:58:24","slug":"paths","status":"publish","type":"post","link":"https:\/\/ibex.tech\/python\/file-input-and-output\/paths","title":{"rendered":"Paths"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Handling path slash character for Windows and Linux compatibility<\/h4>\n\n\n\n<p>Windows uses &#8220;\\\\&#8221;<br>Linux uses &#8220;\/&#8221;<\/p>\n\n\n\n<p>Although using the Linux forwardslash will often work on Windows too, its a bit hacky and isn&#8217;t guaranteed to work. For correct Python code you should use the pathlib to solve it<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>from pathlib import Path\n\n    #Use forward slashes with pathlib functions. Path() object will convert forward slashes into the correct kind of slash for the current operating system:\n    my_path = Path(\"some_folder\/some_sub_folder\/\")\n\n    #You can also used the \/ operator directly in your code to add to a previously created path object:\n    my_path = my_path \/ \"some_file.txt\"<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Detecting Windows or Linux for OS file system dependant path defines<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>import os\n\n\nif os.name == 'nt':\n    DIRECTORY_FILES_ROOT = 'C:\/MyAppDirectory'          #Windows path\nelse:\n    DIRECTORY_FILES_ROOT = '\/home\/pi\/MyAppDirectory'    #Linux path<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Referencing files in the project folder<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#In project folder:\n    Path(\"myimage.jpg\")\n\n#In a sub directory:\n    Path(\"images\/myimage.jpg\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Get path to this files containing folder<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>    our_directory_path = os.path.dirname(os.path.realpath(__file__))\n    file1 = open(Path(our_directory_path + \"\/debug_output.txt\"), \"w\")<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Handling path slash character for Windows and Linux compatibility Windows uses &#8220;\\\\&#8221;Linux uses &#8220;\/&#8221; Although using the Linux forwardslash will often work on Windows too, its a bit hacky and isn&#8217;t guaranteed to work. For correct Python code you should use the pathlib to solve it Detecting Windows or Linux for OS file system dependant [&hellip;]<\/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-4559","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\/4559","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=4559"}],"version-history":[{"count":6,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4559\/revisions"}],"predecessor-version":[{"id":4857,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/posts\/4559\/revisions\/4857"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/media?parent=4559"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/categories?post=4559"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/python\/wp-json\/wp\/v2\/tags?post=4559"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}