The Noobs of Python: Ep.5.0 - File Manipulation

Q: You said I could write my programs for the Big 3 OS's, What
do I need to know upfront?
A: One of the first things you need to know is the Backslash \
used on Windows and the / used on Unix style OS's. If you want your programs
to work on all operating systems, you will have to write your programs to
handle both. Fortunately, this is simple, If you pass it the string values of
individual file and folder names in your path,os.path.join() will return a
string with a file path using the correct path separators.
Open up idle3.5 and try it, This is my example on Linux:

>>> import os
>>> os.path.join('home', 'vindican', 'Downloads')
'home/vindican/Downloads'
>>>

The os.path.join() function is helpful if you need to create strings for
filenames. For example, the following example joins names from a list of
filenames to the end of a folder’s names:

>>> myfiles = ['test.pyt', 'test2.pyt', 'test.pyt']
>>> for filenames in myfiles:
	print(os.path.join('/home/vindican/Downloads', filenames))
	
/home/vindican/Downloads/test.pyt
/home/vindican/Downloads/test2.pyt
/home/vindican/Downloads/test.pyt
>>>

Q: Soooo... What other things can it do?
A: If you are familiar with navigating in a Terminal, there are similar functions
in Python so open up idle3.5 and try out:

>>> import os
>>> os.getcwd()
'/home/vindican'
>>> os.chdir('/home/vindican/Downloads/')
>>> os.getcwd()
'/home/vindican/Downloads'
>>> os.makedirs('/home/vindican/Software/Level1Techs/Lord_Wendell/Kreestah/Grizzle/')
	The last example makes a directory in Software for Level1Techs and sub-directories
	for each Lord_Wendell, Kreestah and Grizzle within. 
>>> os.uname()
posix.uname_result(sysname='Linux', nodename='localhost.localdomain', release='4.8.8-200.fc24.x86_64', 
version='#1 SMP Tue Nov 15 19:41:51 UTC 2016', machine='x86_64')
	this returns the system information
>>>
>>>

Q: So can I open files, maybe read/write to files as well?
A: In this section I'm going to defer some to idle3.5, since it will help in getting used to it's
functionality :so open up idle3.5 and type:

>>> help(open)

Here you will get tons of info on open() and what it can do. reading, writing, appending, creating. These are all features that you can use. The first argument to open() the external filename, may include a platform-specific and absolute or relative directory path prefix. Without a directory path, the file is assumed to exist in the current working directory. In the following example we will place a file in a variable called Level1Tech, then add the .read() method to return the contents of the file. ex:

>>> Level1Tech = open('file1.txt', 'r')
>>> Level1Tech.read()
'Hello Level1Tech !'
>>>

If you've followed along with this series you are familiar with methods, The second argument to open()called processing mode, is typically the string'r' to open for text input (the default), 'w' to create and open for text output, or 'a' to open for appending text to the end of a file. Here are some examples:

>>> import os
>>> os.chdir('/home/vindican/Software/')
>>> os.getcwd()
'/home/vindican/Software'
>>> Level1Tech = open('file1.txt', 'w')
>>> Level1Tech.write('Welcome to The Noobs of Python Ep.5 - Files, here are some interesting things to do.\n')
85
>>> Level1Tech.close()
>>> Level1Tech = open('file1.txt', 'a')
>>> Level1tech.write('This should be the second line if we have done our work properly\n')
65
>>> Level1Tech.close()
>>> Level1Tech = open('file1.txt')
>>> content = Level1Tech.read()
>>> print(content)
Welcome to The Noobs of Python Ep.5 - Files, here are some interesting things to do.
This should be the second line if we have done our work properly

>>>

Q: This should hold me up for a few minutes, what do I have to look forward to afterwards?**
A: It's possible we could cover the shelve module, pprint.pformat() function, File iterators in Ep: 3-.1 - Let's Iterate with I T E R A T I O N S
and many more. There is a ton to cover even going back to previous EP.x's we've started so hang tight.

We are getting closer to dealing with GUI Development, Network Programming, Data Analysis and Forensics with Python including some Encryption tools. So learn all you can now because
'The Knights of Python' series is looming...

Post any comments and questions even suggestions for the direction of the series.

Code on Code_Warriors !!!

5 Likes

Great stuff!

Question, do you have a main post with links to all of your separate EP threads? Would be easier to find/go through all of them

also helps me be lazy to bookmark one thread and watch it, so every addition to the OP would notify me lol

1 Like

the headers has tags noobsofpython python tutorial this way you can follow this series and the knightsofpython series as well

3 Likes