{"id":104,"date":"2020-08-27T16:14:09","date_gmt":"2020-08-27T16:14:09","guid":{"rendered":"http:\/\/japanobserver.net\/?p=104"},"modified":"2020-08-27T18:50:40","modified_gmt":"2020-08-27T18:50:40","slug":"using-python-for-a-redundant-directory-listing","status":"publish","type":"post","link":"https:\/\/japanobserver.net\/index.php\/2020\/08\/27\/using-python-for-a-redundant-directory-listing\/","title":{"rendered":"Using Python for a redundant directory listing"},"content":{"rendered":"<p>A client asked me about help on a Python script. I jumped into my seat and excited typed &#8220;OF COURSE!!&#8221;, more excited than Dug getting to play fetch.<\/p>\n<p>She was using Python to run a DOS command. This isn&#8217;t a horrible way to do things, actually, it is a horrible way to do things so I tried to ween her off this and use a more Pythonic way of doing it. (Okay, full disclosure, I use this for my scripts so they stay live after they&#8217;re finished:)<\/p>\n<p><code>os.system(\"pause\")<\/code><\/p>\n<p>I didn&#8217;t get the entire code, but this was what she eventually came up with that worked:<\/p>\n<p><code>subprocess.call([\"dir\"+\"\/O:-D\/A:-D\",bookingFilePath1, \"&gt;&gt;\",\"tmp2.txt\"], shell=True )<\/code><\/p>\n<p>It&#8217;s cool that she&#8217;s using the subprocess but this isn&#8217;t the &#8220;Python&#8221; way of doing things. At the very least, if you get the result you need without a bug then that should suffice. But being a Pythonista means delving deeper until you come up with the most Python-like solution.<\/p>\n<p>What I came up with is the following, which worked perfectly and is mostly Pythonic:<\/p>\n<p><code>import os<\/p>\n<p>targpath = \"C:\\\\users\\\\ReedActed\\\\desktop\\\\\" # To direct location of target file<\/p>\n<p>with open(targpath + 'P-drive-test.txt', 'w', encoding='utf-8') as outfile:<br \/>\n&emsp;&emsp;for r, d, f in os.walk('P:\\\\backup\\\\'):<br \/>\n&emsp;&emsp;&emsp;&emsp;for file in f:<br \/>\n&emsp;&emsp;&emsp;&emsp;&emsp;fileneat = os.path.join(r, file) + \"\\n\"<br \/>\n&emsp;&emsp;&emsp;&emsp;&emsp;outfile.write(fileneat)<br \/>\n<\/code><\/p>\n<p>Regrettably, she found out what her error was and continued to use the DOS command in Python rather than use Python&#8217;s native libraries. *SIGH*<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A client asked me about help on a Python script. I jumped into my seat and excited typed &#8220;OF COURSE!!&#8221;, more excited than Dug getting to play fetch. She was using Python to run a DOS command. This isn&#8217;t a horrible way to do things, actually, it is a horrible way to do things so &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/japanobserver.net\/index.php\/2020\/08\/27\/using-python-for-a-redundant-directory-listing\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Using Python for a redundant directory listing&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8,9,10],"tags":[12,11,13],"class_list":["post-104","post","type-post","status-publish","format-standard","hentry","category-programming","category-python-programming","category-system-administration","tag-programming","tag-python","tag-system-administration"],"_links":{"self":[{"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/posts\/104","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/comments?post=104"}],"version-history":[{"count":19,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions"}],"predecessor-version":[{"id":116,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/posts\/104\/revisions\/116"}],"wp:attachment":[{"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/media?parent=104"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/categories?post=104"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/japanobserver.net\/index.php\/wp-json\/wp\/v2\/tags?post=104"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}