avatarJim McAulay🍁 I'm nobody. Are you a nobody too?

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

1732

Abstract

ring">"a+"</span>) <span class="hljs-keyword">as</span> f: f.<span class="hljs-built_in">write</span> (add_job+<span class="hljs-string">"\n"</span>)</pre></div><p id="c4ac">The first module creates the job jar in the form of a text file and adds tasks.</p><p id="618d">The first line is not mandatory. It is best practice to create a variable for your text file. If you want to make changes you only have to change one line.</p><p id="0376">In order to use a file you have to open it. The with …as structure is once again not mandatory you could just write open( jar) or without the first line open (job_jar.txt). If you did that you would have to add the line close (jar). The preferred method is to use with which opens the file and then closes it.</p><p id="ad52">The “a” stands for append and the + will create a file if one does not already exist. f is a conventional variable name for file jus as i is used for integer in for i in range (###).</p><p id="d1ed">You need to add a newline to the with +”\n”</p><p id="36fd">If you save this module directly on the desktop the accompanying text file will also appear on the desktop and you can access it manually outside of python.</p><p id="6f17">The second module prints a random task from the jar and then deletes it.</p><div id="65fa"><pre>import <span class="hljs-built_in">random</span> jar = <span class="hljs-string">"job_jar.txt"</span> <span class="hljs-built_in">lines</span> = <span class="hljs-built_in">open</span>(jar).<span class="hljs-built_in">read</span>().splitlines() myline = <span class="hljs-built_in">random</span>.choice(<span class="hljs-built_in">lines</span>) <span class="hljs-built_in">print</span>(<span class="hljs-string">"here's the job to d

Options

ow now"</span>,myline)</pre></div><div id="2615"><pre><span class="hljs-keyword">with</span> <span class="hljs-built_in">open</span>(jar, <span class="hljs-string">"w"</span>) <span class="hljs-keyword">as</span> f: <span class="hljs-keyword">for</span> l <span class="hljs-keyword">in</span> <span class="hljs-keyword">lines</span>: <span class="hljs-keyword">if</span> l != myline: f.<span class="hljs-built_in">write</span>(<span class="hljs-built_in">line</span>+<span class="hljs-string">"\n"</span>) <span class="hljs-function"><span class="hljs-keyword">end</span> = <span class="hljs-title">input</span> ("<span class="hljs-title">tap</span> <span class="hljs-title">any</span> <span class="hljs-title">key</span>")</span></pre></div><p id="50fa">By default when you open a file it is in read only mode “r”. open (jar,”r”)In this case you don’t need to specify it.</p><p id="2f40">You can not remove a line directly. The “w” strands for write and it overwrites content to the file, iterating through the file and omitting the line that you don’t want and once again adding a newline to each entry.</p><p id="3571">If you run this on the desktop with content appearing in the command window the window will close when the program comes to an end.</p><p id="cab8">The last line is just to keep the window open so you can read what job you are going to tackle.</p><h2 id="b901">A note from Plain English</h2><p id="5fdc">Did you know that we have launched a YouTube channel? Every video we make will aim to teach you something new. Check us out by <a href="https://www.youtube.com/channel/UCtipWUghju290NWcn8jhyAw"><b>clicking here</b></a>, and be sure to subscribe to the channel 😎</p></article></body>

File Management With Python

How to create a virtual job jar

Photo by Maksym Kaharlytskyi on Unsplash

Files are named storage components on your computer that are managed by your operating system. You can create and manage such files within Python.

Python can handle both text files and binary files. This piece will deal exclusively with text files which will have the the file extension .txt

Python’s built in open function creates a python file object which serves as a link to a file residing on your machine. If the file does not already exists open can create one.

Once a file is open you can transfer strings of data to and from the file

files are considered a python core type since they can be created by a built in function however they can also exist external from python.

files are not numbers or sequences and do not respond to expression operators

They export methods only for common file processing tasks.

In this article we are going to write two modules to simulate a virtual job jar using python.

jar = "job_jar.txt"
add_job = input ("add a job  ")
with open (jar,"a+") as f:
     f.write (add_job+"\n")

The first module creates the job jar in the form of a text file and adds tasks.

The first line is not mandatory. It is best practice to create a variable for your text file. If you want to make changes you only have to change one line.

In order to use a file you have to open it. The with …as structure is once again not mandatory you could just write open( jar) or without the first line open (job_jar.txt). If you did that you would have to add the line close (jar). The preferred method is to use with which opens the file and then closes it.

The “a” stands for append and the + will create a file if one does not already exist. f is a conventional variable name for file jus as i is used for integer in for i in range (###).

You need to add a newline to the with +”\n”

If you save this module directly on the desktop the accompanying text file will also appear on the desktop and you can access it manually outside of python.

The second module prints a random task from the jar and then deletes it.

import random
jar = "job_jar.txt"
lines = open(jar).read().splitlines()
myline = random.choice(lines)
print("here's the job to dow now",myline)
with open(jar, "w") as f:
    for l in lines:
        if l != myline:
            f.write(line+"\n")
end = input ("tap any key")

By default when you open a file it is in read only mode “r”. open (jar,”r”)In this case you don’t need to specify it.

You can not remove a line directly. The “w” strands for write and it overwrites content to the file, iterating through the file and omitting the line that you don’t want and once again adding a newline to each entry.

If you run this on the desktop with content appearing in the command window the window will close when the program comes to an end.

The last line is just to keep the window open so you can read what job you are going to tackle.

A note from Plain English

Did you know that we have launched a YouTube channel? Every video we make will aim to teach you something new. Check us out by clicking here, and be sure to subscribe to the channel 😎

Python
File Management
Python Programming
Beginner Guide To Python
Jims Python
Recommended from ReadMedium