<SYSTEM_TASK:>
Given the following text description, write Python code to implement the functionality described below step by step
<END_TASK>
<USER_TASK:>
Description:
 Lesson 30
Step1:  But this string only works on Windows; to create an OS insensitive path, using the os module
Step2:  If no explicit path is specified, Python will look for files in the current working directory. 
You can find out what the current working directory is with os.getcwd(), and change it is with os.chdir().
Step3:  There are two kinds of file paths, relative and absolute.
Absolute file paths are the full address, while relative paths are related to the current working directory; they start at the cwd, not the root folder.
There are also . and .. operators
Step4:  os.path.dirname() pulls out just the directory path component above a filepath.
os.path.basename() pulls out whats past the last slash.
Step5:  os.path.exists() can check if a path exists.
os.path.isfile() checks if the path ends at a file
os.path.isdir() checks if the path ends at a folder.
Step7:  os.path.getsize() returns the size of a file (bytes)
os.path.listdir() returns a list of all the files at the path. 
File Size Finder
Step8:  os.makedirs() creates new directories at the location.
os.removedirs() removes folders in an absolute location.
<ASSISTANT_TASK:>
Python Code:
'\\'.join(['folder1','folder2','folder3','file.png']) # join all elements using the escaped (literal) '\' string
Explanation: Lesson 30:
Filenames and Absolute/Relative File Paths
To make sure a programs output persists, scripts have to save to files.
Filenames and File Paths
Files are held in Folders. 
A Folder is just a directory on the disk.
A File Path is the path to that file through those folders.
All file paths start at the Root folder (/ on UNIX systems and C:\ on Windows)
Unix systems use /, Windows systems use \. 
Files also have File Extensions which is the .type suffix; it tells the OS what application handles the file. 
One way to construct file paths is the .join string method:
End of explanation
import os # contains many file path related functions
print(os.path.join('folder1','folder2','folder3','file.png')) # takes string arguments and returns OS-appropriate path
print(os.sep) # show the seperator currently being used.
Explanation: But this string only works on Windows; to create an OS insensitive path, using the os module:
End of explanation
# Start at current directory
defaultpath = os.path.expanduser('~/Dropbox/learn/books/Python/AutomateTheBoringStuffWithPython/')
os.chdir(defaultpath)
print(os.getcwd())
# Change path to /files folder
os.chdir('files') # changes current working directory, if not currently in it (to /files)
print(os.getcwd()) # prints the current working directory (should be /files)
# Reset back to notebook directory
os.chdir(defaultpath)
Explanation: If no explicit path is specified, Python will look for files in the current working directory. 
You can find out what the current working directory is with os.getcwd(), and change it is with os.chdir().
End of explanation
print(os.path.abspath('files')) # print absolute path of the files subdirectory
print(os.path.isabs(os.path.abspath('files'))) # Is the absolute path to files an absolute path (True)
print(os.path.relpath('../..', 'files')) # print the relative file path of a folder two folders up relative to subfolder (3 folders; ../../..)
Explanation: There are two kinds of file paths, relative and absolute.
Absolute file paths are the full address, while relative paths are related to the current working directory; they start at the cwd, not the root folder.
There are also . and .. operators:
* .\ refers to the cwd, and .\path will look at any folders below this folder.
* ..\ refers to the folder above the cwd, and will look at any folders below the parent folder of the cwd.
os.path.abspath returns the absolute path of whatever relative path is passed to it.
os.path.relpath() returns the relative path from an absolute path to a relative location.
os.path.isabs() returns a boolean if the path is a true path.
End of explanation
print(os.path.dirname(os.path.abspath('files'))) # outputs absolute path above 'files'
print(os.path.basename('files/26645.pdf')) # outputs just 'files'
Explanation: os.path.dirname() pulls out just the directory path component above a filepath.
os.path.basename() pulls out whats past the last slash.
End of explanation
# Reset back to notebook directory
os.chdir(defaultpath)
print(os.path.exists(os.path.abspath('files'))) # checks if 'files' exists (True)
print(os.path.isfile('files')) # checks if 'files' is a file (False)
print(os.path.isdir('files')) # checks if 'files' is a folder (False)
Explanation: os.path.exists() can check if a path exists.
os.path.isfile() checks if the path ends at a file
os.path.isdir() checks if the path ends at a folder.
End of explanation
A simple program to loop through a folder and find the size of all files in bytes, and the total size of the folder.
import os
# starting size
totalSize = 0 
# for the fileName in the 'files' directory
for fileName in os.listdir('files'):
    # generate filePaths
    filePath = os.path.join('files',fileName) 
    # check if filePath is a file
    if os.path.isfile(filePath) == True:
    # if True, increase totalSize by the size of fileName
        totalSize += os.path.getsize(filePath)
    # also print what the file was and the size
        print('%s is %d bytes.'%(filePath, os.path.getsize(filePath)))
    # otherwise keep looping
    else:
        continue
# print the size of the folder at the end
print('\n\nThe \'%s\' folder contains %s bytes in total.'%('files',str(totalSize)))
Explanation: os.path.getsize() returns the size of a file (bytes)
os.path.listdir() returns a list of all the files at the path. 
File Size Finder
End of explanation
# clear the folders if the exist already
if os.path.exists(os.path.abspath('files/newfolder/anotherone')) == True:
    os.removedirs(os.path.abspath('files/newfolder/anotherone')) # clear folders if they exist
# create new folders at an absolute path
os.makedirs(os.path.abspath('files/newfolder/anotherone')) # create new folders
# check if they exist
if os.path.exists(os.path.abspath('files/newfolder/anotherone')) == True:
    print('\'files/newfolder/anotherone\' exists.')
Explanation: os.makedirs() creates new directories at the location.
os.removedirs() removes folders in an absolute location.
End of explanation
<END_TASK>