[Tut] How to Open Multiple Files in Python - Printable Version +- Sick Gaming (https://www.sickgaming.net) +-- Forum: Programming (https://www.sickgaming.net/forum-76.html) +--- Forum: Python (https://www.sickgaming.net/forum-83.html) +--- Thread: [Tut] How to Open Multiple Files in Python (/thread-99550.html) |
[Tut] How to Open Multiple Files in Python - xSicKxBot - 06-11-2022 How to Open Multiple Files in Python <div><div class="kk-star-ratings kksr-valign-top kksr-align-left " data-payload="{"align":"left","id":"403242","slug":"default","valign":"top","reference":"auto","count":"0","readonly":"","score":"0","best":"5","gap":"5","greet":"Rate this post","legend":"0\/5 - (0 votes)","size":"24","width":"0","_legend":"{score}\/{best} - ({count} {votes})"}"> <div class="kksr-stars"> <div class="kksr-stars-inactive"> <div class="kksr-star" data-star="1" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="2" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="3" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="4" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" data-star="5" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> <div class="kksr-stars-active" style="width: 0px;"> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> <div class="kksr-star" style="padding-right: 5px"> <div class="kksr-icon" style="width: 24px; height: 24px;"></div> </p></div> </p></div> </div> <div class="kksr-legend"> <span class="kksr-muted">Rate this post</span> </div> </div> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">In this article, you’ll learn how to open multiple files in Python.</p> <h2 class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Problem Formulation and Solution Overview</h2> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio has-global-color-8-background-color has-background"><em><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4ac.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Question</strong>: How would we write Python code to open multiple files?</em></p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">We can accomplish this task by one of the following options:</p> <ul type="video" class="wp-embed-aspect-16-9 wp-has-aspect-ratio"> <li><strong>Method 1</strong>: Open Multiple Text Files using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="URL" data-id="https://blog.finxter.com/python-open-function/" target="_blank"><code>open()</code></a></li> <li><strong>Method 2</strong>:Open Multiple Text Files using <a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="URL" data-id="https://blog.finxter.com/python-open-function/" target="_blank"><code>open()</code></a> and backslash (<code>\</code>)</li> <li><strong>Method 3</strong>: Open Multiple Text Files using Parenthesized Context Managers and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" target="_blank"><code>open()</code></a></li> <li><strong>Method 4</strong>: Open Multiple Text Files using the <a rel="noreferrer noopener" href="https://docs.python.org/3/library/os.html" data-type="URL" data-id="https://docs.python.org/3/library/os.html" target="_blank">os</a> library and <a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="URL" data-id="https://blog.finxter.com/python-open-function/" target="_blank"><code>open()</code></a></li> </ul> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">To make it more fun, we have the following running scenario:</p> <p><em>You have been contacted by the <a rel="noreferrer noopener" href="https://academy.finxter.com/" data-type="URL" data-id="https://academy.finxter.com/" target="_blank">Finxter Academy</a> to write code that opens multiple files and writes the contents from one file to another.</em></p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Preparation & Overview</h2> <p>If your script opens files, it is a good practice to check for errors, such as:</p> <ul> <li><em>No Such File Or Directory</em></li> <li><em>Not Readable</em></li> <li><em>Not Writable</em></li> </ul> <p> In this regard, all examples in this article will be wrapped in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-try-except-an-illustrated-guide/" data-type="URL" data-id="https://blog.finxter.com/python-try-except-an-illustrated-guide/" target="_blank"><code>try/except</code></a> statement to catch these errors.</p> <p><strong>Contents of <code>orig_file.txt</code></strong></p> <p>To follow along with this article, create <code>orig_file.txt</code> with the contents below and place it in the current working directory.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>30022425,Oliver,Grandmaster,2476<br />30022437,Cindy,Intermediate,1569<br />30022450,Leon,Authority,1967</code></td> </tr> </tbody> </table> </figure> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.</p> <pre class="EnlighterJSRAW wp-embed-aspect-16-9 wp-has-aspect-ratio" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import logging</pre> <p>This allows us to log any error message that may occur when handling the files.</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 1: Open Multiple Text Files with open()</h2> <p class="has-global-color-8-background-color has-background">This method opens multiple text files simultaneously on <strong>one</strong> line using the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-open-function/" data-type="URL" data-id="https://blog.finxter.com/python-open-function/" target="_blank"><code>open()</code></a> statement separated by the comma (<code>,</code>) character. </p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">try: with open('orig_file.txt', 'r') as fp1, open('new_file.txt', 'w') as fp2: fp2.write(fp1.read()) except OSError as error: logging.error("This Error Occurred: %s", error)</pre> <p>This code snippet is wrapped in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-try-except-an-illustrated-guide/" data-type="URL" data-id="https://blog.finxter.com/python-try-except-an-illustrated-guide/" target="_blank"><code>try/except</code></a> statement to catch errors. When this runs, it falls inside the <code>try</code> statement and executes as follows:</p> <ul> <li>Opens two (2) text files on <strong>one </strong>line of code. <ul> <li>The file for reading, which contains a comma (<code>,</code>) character at the end to let Python know there is another file to open: <code>open('orig_file.txt', 'r') as fp1</code></li> <li>The second is for writing: <code>open('new_file.txt', 'w') as fp2</code></li> </ul> </li> <li>Then, the entire contents of <code>orig_file.txt</code> writes to <code>new_file.txt</code>.</li> </ul> <p>If an error occurs during the above process, the code falls to the <code>except</code> statement, which retrieves and outputs the error message to the terminal.</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-strip/" data-type="URL" data-id="https://blog.finxter.com/python-string-strip/" target="_blank"><code>strip()</code></a> function removes any trailing space characters.<br />The newline character (<code>\n</code>) is added to place each iteration on its own line. </p> <p>If successful, the contents of <code>orig_file.txt</code> and <code>new_file.txt</code> are identical.</p> <p><strong>Output of new_file.txt</strong></p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>30022425,Oliver,Grandmaster,2476<br />30022437,Cindy,Intermediate,1569<br />30022450,Leon,Authority,1967</code></td> </tr> </tbody> </table> </figure> <figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"> <div class="wp-block-embed__wrapper"> <iframe loading="lazy" title="Python open() Function -- An 80/20 Guide by Example" width="780" height="439" src="https://www.youtube.com/embed/so9MlRnUG1Y?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe> </div> </figure> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 2: Open Files Across Multiple Lines with open() and Backslash (\)</h2> <p class="has-global-color-8-background-color has-background">Before <a href="https://blog.finxter.com/how-to-check-your-python-version/" data-type="post" data-id="1371" target="_blank" rel="noreferrer noopener">Python version</a> <a href="https://blog.finxter.com/python-version-history/" data-type="post" data-id="35256" target="_blank" rel="noreferrer noopener">3.10</a>, and in use today, opening multiple files on one line could be awkward. To circumvent this, use the backslash (\) character as shown below to place the <code>open()</code> statements on separate lines.</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2-3" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">try: with open('orig_file.txt', 'r') as fp1, \ open('new_file3.txt', 'w') as fp2: fp2.write(fp1.read()) except OSError as error: logging.error("This Error Occurred: %s", error)</pre> <p>This code snippet is wrapped in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-try-except-an-illustrated-guide/" data-type="URL" data-id="https://blog.finxter.com/python-try-except-an-illustrated-guide/" target="_blank"><code>try/except</code></a> statement to catch errors. When this runs, it falls inside the <code>try</code> statement and executes as follows:</p> <ul> <li>Opens the first file using <code>open('orig_file.txt', 'r')</code> for reading and contains a backslash (<code>\</code>) character to let Python know there is another file to open.</li> <li>Opens the second file using <code>open('new_file.txt', 'w')</code> for writing.</li> <li>Then, the entire contents of <code>orig_file.txt</code> writes to <code>new_file.txt</code>.</li> </ul> <p>If an error occurs during the above process, the code falls to the <code>except</code> statement, which retrieves and outputs the error message to the terminal.</p> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: The <a rel="noreferrer noopener" href="https://blog.finxter.com/python-string-strip/" data-type="URL" data-id="https://blog.finxter.com/python-string-strip/" target="_blank"><code>strip()</code></a> function removes any trailing space characters.<br />The newline character (<code>\n</code>) is added to place each iteration on its own line. </p> <p>If successful, the contents of <code>orig_file.txt</code> and <code>new_file.txt</code> are identical.</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 3: Open Multiple Text Files using Parenthesized Context Managers and open()</h2> <p class="has-global-color-8-background-color has-background">In Python version 3.10, Parenthesized Context Managers were added. This fixes a bug found in version 3.9, which did not support the use of parentheses across multiple lines of code. How Pythonic!</p> <p>Here’s how they look in a short example:</p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="2-5" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">try: with ( open('orig_file.txt', 'r') as fp1, open('new_file.txt', 'w') as fp2 ): fp2.write(fp1.read()) except OSError as error: logging.error("This Error Occurred: %s", error) </pre> <p>This code snippet is wrapped in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-try-except-an-illustrated-guide/" data-type="URL" data-id="https://blog.finxter.com/python-try-except-an-illustrated-guide/" target="_blank"><code>try/except</code></a> statement to catch errors. When this runs, it falls inside the <code>try</code> statement and executes as follows:</p> <ul> <li>Declare the opening of <code>with</code> and the opening bracket (<code>with (</code>)). <ul> <li>Opens <code>orig_file.txt</code> (<code>open('orig_file.txt', 'r') as fp1,</code>) for reading with a comma (,) to let Python know to expect another file.</li> <li>Open <code>new_file.txt</code> (<code>open('new_file.txt', 'w') as fp2</code>) for writing.</li> </ul> </li> <li>Closes the <code>with</code> statement by using <code>):</code>.</li> <li>Then, the entire contents of <code>orig_file.txt</code> writes to <code>new_file.txt</code>.</li> </ul> <p>If an error occurs during the above process, the code falls to the <code>except</code> statement, which retrieves and outputs the error message to the terminal.</p> <p>If successful, the contents of <code>orig_file.txt</code> and <code>new_file.txt</code> are identical.</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Method 4: Open Multiple Text Files using the os library and open()</h2> <p class="has-global-color-8-background-color has-background">This method calls in the <code><a rel="noreferrer noopener" href="https://docs.python.org/3/library/os.html" data-type="URL" data-id="https://docs.python.org/3/library/os.html" target="_blank">os</a></code> library (<code>import os</code>) which provides functionality to work with the Operating System. Specifically, for this example, file and folder manipulation. </p> <pre class="EnlighterJSRAW" data-enlighter-language="python" data-enlighter-theme="" data-enlighter-highlight="8" data-enlighter-linenumbers="" data-enlighter-lineoffset="" data-enlighter-title="" data-enlighter-group="">import os os.chdir('files') filelist = os.listdir(os.getcwd()) for i in filelist: try: with open(i, 'r') as f: for line in f.readlines(): print(line) except OSError as error: print('error %s', error) </pre> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/1f4a1.png" alt="?" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <strong>Note</strong>: In this example, two (2) files are read in and <strong>output</strong> to the terminal.</p> <p>This code snippet imports the <code><a rel="noreferrer noopener" href="https://docs.python.org/3/library/os.html" data-type="URL" data-id="https://docs.python.org/3/library/os.html" target="_blank">os</a></code> library to access the required functions.</p> <p>For this example, we have two (2) text files located in our <code>files</code> directory:<br /><code>file1.txt</code> and <code>file2.txt.</code> To access and work with these files, we call <code>os.chdir('files')</code> to change to this folder (directory).</p> <p>Next, we retrieve a list of all files residing in the current working directory <br />(<code>os.listdir(os.getcwd()</code>) and save the results to <code>filelist</code>.</p> <p>IF we output the contents of <code>filelist</code> to the terminal, we would have the following: a list of all files in the current working directory in a <a href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank" rel="noreferrer noopener">List</a> format.</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>['file1.txt', 'file2.txt']</code></td> </tr> </tbody> </table> </figure> <p>This code snippet is wrapped in a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-try-except-an-illustrated-guide/" data-type="URL" data-id="https://blog.finxter.com/python-try-except-an-illustrated-guide/" target="_blank"><code>try/except</code></a> statement to catch errors. When this runs, it falls inside the <code>try</code> statement and executes as follows:</p> <ul> <li>Instantiates a <a rel="noreferrer noopener" href="https://blog.finxter.com/python-loops/" data-type="URL" data-id="https://blog.finxter.com/python-loops/" target="_blank"><code>for</code></a> loop to traverse through each file in the <a rel="noreferrer noopener" href="https://blog.finxter.com/python-lists/" data-type="URL" data-id="https://blog.finxter.com/python-lists/" target="_blank">List</a> and does the following: <ul> <li>Opens the current file for reading.</li> <li>Reads in this file one line at a time and output to the terminal.</li> </ul> </li> </ul> <p>If an error occurs during the above process, the code falls to the <code>except</code> statement, which retrieves and outputs the error message to the terminal.</p> <p><strong>Output</strong></p> <p>The contents of the two (2) files are:</p> <figure class="wp-block-table is-style-stripes"> <table> <tbody> <tr> <td><code>Contents of File1.<br />Contents of File2.</code></td> </tr> </tbody> </table> </figure> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Summary</h2> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">These four (4) methods of how to multiple files should give you enough information to select the best one for your coding requirements.</p> <p class="wp-embed-aspect-16-9 wp-has-aspect-ratio">Good Luck & Happy Coding!</p> <hr class="wp-block-separator has-alpha-channel-opacity"/> <h2>Programmer Humor</h2> <p class="has-global-color-8-background-color has-background"><img src="https://s.w.org/images/core/emoji/14.0.0/72x72/2753.png" alt="❓" class="wp-smiley" style="height: 1em; max-height: 1em;" /> <code><strong>Question:</strong> How did the programmer <strong><em>die</em></strong> in the shower? ☠</p> <p>❗ <strong>Answer</strong>: They read the shampoo bottle instructions: <br /><em><strong>Lather. Rinse. Repeat.</strong></em></code></p></p> </div> https://www.sickgaming.net/blog/2022/06/06/how-to-open-multiple-files-in-python/ |