Files and Functions
Day 1 : 15 days of Python
Update: I upload all my lpthw code to Github here (Study drills included).
Exercise 17 wasn’t too difficult to understand. Here, all program (or script, I like to call it a program) does, is copy contents of one file (let it be file1) to the other (let it be file2). Yes, that’s it. It just copies contents from one file to another file which may or may not be empty. If the file (file2) isn’t empty and contains some important information (why would you do that!?) then boy, oh boy, you’re in big trouble!
That’s because the file to which the contents of other file is copied (i.e. file2) is being opened in ‘write’ mode. What write mode does is, it first deletes all of the things which were present in the file already, and then writes the stuff which its supposed to. In this case, replicate the contents of an other file.
When you run the program again and again, you really can’t see what’s the difference before and after running it (if you’re using same file each time to copy the contents from). So I wrote the script such that the file opens in ‘append’ mode which is denoted by an ‘a’ instead of ‘w’. In append mode, the newly added stuff is added right after where the contents of the file ends.
About the ‘echo’ and ‘cat’ thing, you don’t need to learn that. It’s just a fancier way of creating a file and then putting some text in it which you can do without the command line.
Oh and I totally forgot to tell you about a ‘not so big’ problem I had (but it was quite frustrating). While following the Study drills, I encountered something. It was not an error, rather it was an anomaly in how the windows saves a .txt file (at least I think so). On running the program, it copied the text from the test.txt file but what appeared in new_file.txt was in Japanese!! I tried to figure out myself what the problem was but failed. Eventually I found out that it was an encoding problem! new_file.txt was encoded UTF-16 and test.txt was UTF-8 encoded. So to solve that problem I had to convert it to UTF-8.
Exercise 18 is very, very easy. Its just about creating functions (or as the author says, mini-scripts). Nothing much to talk about here except that ‘*args’ thing. I was quite confused after seeing *args and i mistook it for a pointer (it looks just like this, more on this later), but soon i figured it out and realized that it’s just like argv which contains just a list of arguments. Study drill for this exercise is totally worth it. I highly recommend doing it exactly as the author says.
Exercise 19 is more or less like exercise 18. The difference is that in this exercise, author tells different ways to call a function. The real challenge is the Study drills 3 of this exercise. I tried to create a function to do simple addition yet I failed several times. Here are some of the screen shots of my errors (Hi five if you got the same!).
As you can see, finding bugs could be quite exhausting and frustrating (it took me about 20–30 minutes to figure out the solution to that error!!). You just have to be focused, dedicated and most importantly, calm, to figure something out.
Thanks for reading this article! An article is published daily on this series (15 Days of Python), so stay tuned for more. If you liked this post, please consider sharing it with others and recommending with a heart. ❤
Hello guys! I’m Ashutosh Pathak, and I’m an undergrad student from India. I’m learning all sorts of stuff from the web. I am most fascinated by Machine Learning and its applications in real world. This is my journey towards learning, understanding and implementing ML models, all by myself. So come and hop along, for an unforgettable adventure!