Skip to content

Commit 0b18e57

Browse files
AlanAlan
authored andcommitted
Handling Files, Directories, and Data
1 parent b31e994 commit 0b18e57

16 files changed

Lines changed: 78 additions & 1 deletion

Chapter05/Haltermanpythonbook.pdf

3.28 MB
Binary file not shown.

Chapter05/compare_data.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import pandas as pd
2+
3+
df1 = pd.read_csv('student1.csv')
4+
df2 = pd.read_csv('student2.csv')
5+
s1 = set([tuple(values) for values in df1.values.tolist()])
6+
s2 = set([tuple(values) for values in df2.values.tolist()])
7+
s1.symmetric_difference(s2)
8+
print(pd.DataFrame(list(s1.difference(s2))), '\n')
9+
print(pd.DataFrame(list(s2.difference(s1))), '\n')

Chapter05/compress_a_directory.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import shutil
2+
shutil.make_archive('work', 'zip', 'work/')
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import tarfile
2+
tar_file = tarfile.open('work.tar.gz', 'r:gz')
3+
print(tar_file.getnames())

Chapter05/hello.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
print("")
2+
print("Hello World\n")
3+
print("Hello Python\n")

Chapter05/merge_data.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import pandas as pd
2+
3+
df1 = pd.read_csv('student1.csv')
4+
df2 = pd.read_csv('student2.csv')
5+
result = pd.concat([df1, df2])
6+
print(result)

Chapter05/metadata_example.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import PyPDF2
2+
3+
def main():
4+
file_name = 'Haltermanpythonbook.pdf'
5+
pdfFile = PyPDF2.PdfFileReader(open(file_name, 'rb'))
6+
pdf_data = pdfFile.getDocumentInfo()
7+
print('----Metadata of the file----')
8+
for md in pdf_data:
9+
print(md+ ':' +pdf_data[md])
10+
11+
if __name__ == '__main__':
12+
main()

Chapter05/pattern_match.py

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import glob
2+
3+
file_match = glob.glob('*.txt')
4+
print(file_match)
5+
file_match = glob.glob('[0-9].txt')
6+
print(file_match)
7+
file_match = glob.glob('**/*.txt', recursive=True)
8+
print(file_match)
9+
file_match = glob.glob('**/', recursive=True)
10+
print(file_match)

Chapter05/shutil_copy_example.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import shutil
2+
import os
3+
4+
shutil.copy('hello.py', 'welcome.py')
5+
print('Copy Successful')

Chapter05/shutil_move_example.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import shutil
2+
shutil.move('/home/vagrant/sample.txt', '/home/vagrant/Desktop/.')

0 commit comments

Comments
 (0)