|
103 | 103 | - [Writing to Files](#writing-to-files) |
104 | 104 | - [Saving Variables with the shelve Module](#saving-variables-with-the-shelve-module) |
105 | 105 | - [Saving Variables with the pprint.pformat() Function](#saving-variables-with-the-pprintpformat-function) |
| 106 | + - [Copying Files and Folders](#copying-files-and-folders) |
| 107 | + - [Moving and Renaming Files and Folders](#moving-and-renaming-files-and-folders) |
| 108 | + - [Permanently Deleting Files and Folders](#permanently-deleting-files-and-folders) |
| 109 | + - [Safe Deletes with the send2trash Module](#safe-deletes-with-the-send2trash-module) |
| 110 | + - [Walking a Directory Tree](#walking-a-directory-tree) |
| 111 | + - [Reading ZIP Files](#reading-zip-files) |
| 112 | + - [Extracting from ZIP Files](#extracting-from-zip-files) |
| 113 | + - [Creating and Adding to ZIP Files](#creating-and-adding-to-zip-files) |
106 | 114 |
|
107 | 115 | ## Python Basics |
108 | 116 |
|
@@ -2053,4 +2061,181 @@ Just like dictionaries, shelf values have keys() and values() methods that will |
2053 | 2061 | 83 |
2054 | 2062 |
|
2055 | 2063 | >>> fileObj.close() |
2056 | | -``` |
| 2064 | +``` |
| 2065 | + |
| 2066 | +### Copying Files and Folders |
| 2067 | + |
| 2068 | +The shutil module provides functions for copying files, as well as entire folders. |
| 2069 | + |
| 2070 | +```python |
| 2071 | +>>> import shutil, os |
| 2072 | + |
| 2073 | +>>> os.chdir('C:\\') |
| 2074 | + |
| 2075 | +>>> shutil.copy('C:\\spam.txt', 'C:\\delicious') |
| 2076 | + 'C:\\delicious\\spam.txt' |
| 2077 | + |
| 2078 | +>>> shutil.copy('eggs.txt', 'C:\\delicious\\eggs2.txt') |
| 2079 | + 'C:\\delicious\\eggs2.txt' |
| 2080 | +``` |
| 2081 | + |
| 2082 | +While shutil.copy() will copy a single file, shutil.copytree() will copy an entire folder and every folder and file contained in it: |
| 2083 | + |
| 2084 | +```python |
| 2085 | +>>> import shutil, os |
| 2086 | + |
| 2087 | +>>> os.chdir('C:\\') |
| 2088 | + |
| 2089 | +>>> shutil.copytree('C:\\bacon', 'C:\\bacon_backup') |
| 2090 | +'C:\\bacon_backup' |
| 2091 | +``` |
| 2092 | + |
| 2093 | +### Moving and Renaming Files and Folders |
| 2094 | + |
| 2095 | +```python |
| 2096 | +>>> import shutil |
| 2097 | +>>> shutil.move('C:\\bacon.txt', 'C:\\eggs') |
| 2098 | +'C:\\eggs\\bacon.txt' |
| 2099 | +``` |
| 2100 | + |
| 2101 | +The destination path can also specify a filename. In the following example, the source file is moved and renamed: |
| 2102 | + |
| 2103 | +```python |
| 2104 | +>>> shutil.move('C:\\bacon.txt', 'C:\\eggs\\new_bacon.txt') |
| 2105 | +'C:\\eggs\\new_bacon.txt' |
| 2106 | +``` |
| 2107 | + |
| 2108 | + If there is no eggs folder, then move() will rename bacon.txt to a file named eggs. |
| 2109 | + |
| 2110 | +```python |
| 2111 | +>>> shutil.move('C:\\bacon.txt', 'C:\\eggs') |
| 2112 | +'C:\\eggs' |
| 2113 | +``` |
| 2114 | + |
| 2115 | +### Permanently Deleting Files and Folders |
| 2116 | + |
| 2117 | +- Calling os.unlink(path) will delete the file at path. |
| 2118 | + |
| 2119 | +- Calling os.rmdir(path) will delete the folder at path. This folder must be empty of any files or folders. |
| 2120 | + |
| 2121 | +- Calling shutil.rmtree(path) will remove the folder at path, and all files and folders it contains will also be deleted. |
| 2122 | + |
| 2123 | +### Safe Deletes with the send2trash Module |
| 2124 | + |
| 2125 | + You can install this module by running pip install send2trash from a Terminal window. |
| 2126 | + |
| 2127 | +```python |
| 2128 | +>>> import send2trash |
| 2129 | + |
| 2130 | +>>> baconFile = open('bacon.txt', 'a') # creates the file |
| 2131 | + |
| 2132 | +>>> baconFile.write('Bacon is not a vegetable.') |
| 2133 | +25 |
| 2134 | + |
| 2135 | +>>> baconFile.close() |
| 2136 | + |
| 2137 | +>>> send2trash.send2trash('bacon.txt') |
| 2138 | +``` |
| 2139 | + |
| 2140 | +### Walking a Directory Tree |
| 2141 | + |
| 2142 | +```python |
| 2143 | +import os |
| 2144 | + |
| 2145 | +for folderName, subfolders, filenames in os.walk('C:\\delicious'): |
| 2146 | + print('The current folder is ' + folderName) |
| 2147 | + |
| 2148 | + for subfolder in subfolders: |
| 2149 | + print('SUBFOLDER OF ' + folderName + ': ' + subfolder) |
| 2150 | + for filename in filenames: |
| 2151 | + print('FILE INSIDE ' + folderName + ': '+ filename) |
| 2152 | + |
| 2153 | + print('') |
| 2154 | +``` |
| 2155 | + |
| 2156 | +Output: |
| 2157 | + |
| 2158 | +```python |
| 2159 | +The current folder is C:\delicious |
| 2160 | +SUBFOLDER OF C:\delicious: cats |
| 2161 | +SUBFOLDER OF C:\delicious: walnut |
| 2162 | +FILE INSIDE C:\delicious: spam.txt |
| 2163 | + |
| 2164 | +The current folder is C:\delicious\cats |
| 2165 | +FILE INSIDE C:\delicious\cats: catnames.txt |
| 2166 | +FILE INSIDE C:\delicious\cats: zophie.jpg |
| 2167 | + |
| 2168 | +The current folder is C:\delicious\walnut |
| 2169 | +SUBFOLDER OF C:\delicious\walnut: waffles |
| 2170 | + |
| 2171 | +The current folder is C:\delicious\walnut\waffles |
| 2172 | +FILE INSIDE C:\delicious\walnut\waffles: butter.txt. |
| 2173 | +``` |
| 2174 | + |
| 2175 | +### Reading ZIP Files |
| 2176 | + |
| 2177 | +```python |
| 2178 | +>>> import zipfile, os |
| 2179 | + |
| 2180 | +>>> os.chdir('C:\\') # move to the folder with example.zip |
| 2181 | + |
| 2182 | +>>> exampleZip = zipfile.ZipFile('example.zip') |
| 2183 | + |
| 2184 | +>>> exampleZip.namelist() |
| 2185 | +['spam.txt', 'cats/', 'cats/catnames.txt', 'cats/zophie.jpg'] |
| 2186 | + |
| 2187 | +>>> spamInfo = exampleZip.getinfo('spam.txt') |
| 2188 | + |
| 2189 | +>>> spamInfo.file_size |
| 2190 | +13908 |
| 2191 | + |
| 2192 | +>>> spamInfo.compress_size |
| 2193 | +3828 |
| 2194 | + |
| 2195 | +>>> 'Compressed file is %sx smaller!' % (round(spamInfo.file_size / spamInfo.compress_size, 2)) |
| 2196 | +'Compressed file is 3.63x smaller!' |
| 2197 | + |
| 2198 | +>>> exampleZip.close() |
| 2199 | +``` |
| 2200 | + |
| 2201 | +### Extracting from ZIP Files |
| 2202 | + |
| 2203 | +The extractall() method for ZipFile objects extracts all the files and folders from a ZIP file into the current working directory. |
| 2204 | + |
| 2205 | +```python |
| 2206 | +>>> import zipfile, os |
| 2207 | + |
| 2208 | +>>> os.chdir('C:\\') # move to the folder with example.zip |
| 2209 | + |
| 2210 | +>>> exampleZip = zipfile.ZipFile('example.zip') |
| 2211 | + |
| 2212 | +>>> exampleZip.extractall() |
| 2213 | + |
| 2214 | +>>> exampleZip.close() |
| 2215 | +``` |
| 2216 | + |
| 2217 | +The extract() method for ZipFile objects will extract a single file from the ZIP file. Continue the interactive shell example: |
| 2218 | + |
| 2219 | +```python |
| 2220 | +>>> exampleZip.extract('spam.txt') |
| 2221 | +'C:\\spam.txt' |
| 2222 | + |
| 2223 | +>>> exampleZip.extract('spam.txt', 'C:\\some\\new\\folders') |
| 2224 | +'C:\\some\\new\\folders\\spam.txt' |
| 2225 | + |
| 2226 | +>>> exampleZip.close() |
| 2227 | +``` |
| 2228 | + |
| 2229 | +### Creating and Adding to ZIP Files |
| 2230 | + |
| 2231 | +```python |
| 2232 | +>>> import zipfile |
| 2233 | + |
| 2234 | +>>> newZip = zipfile.ZipFile('new.zip', 'w') |
| 2235 | + |
| 2236 | +>>> newZip.write('spam.txt', compress_type=zipfile.ZIP_DEFLATED) |
| 2237 | + |
| 2238 | +>>> newZip.close() |
| 2239 | +``` |
| 2240 | + |
| 2241 | +This code will create a new ZIP file named new.zip that has the compressed contents of spam.txt. |
0 commit comments