Skip to content

Commit b9794ab

Browse files
committed
Merge pull request Show-Me-the-Code#87 from razzl/master
razzl
2 parents 2e5c396 + 94d6cfd commit b9794ab

File tree

2 files changed

+37
-0
lines changed

2 files changed

+37
-0
lines changed

razzl/0004/0004.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
'''
2+
It can caculate the words in the text file
3+
'''
4+
5+
import re
6+
def calculate_words(path):
7+
f = open(path,'r')
8+
lines = f.readlines()
9+
count = 0
10+
for line in lines:
11+
count+=len(re.split('[,.! ?:]',line))#use the re module to split the txt file
12+
return count-len(lines)#the txt file will inlcude the '\n' and '' so sub it
13+
14+
words = calculate_words("C:/Users/razzl/Desktop/1.txt")#in python the '/' can be the path separator in all system
15+
print words

razzl/0005/0005.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
'''
2+
It can resize the photos in a file
3+
'''
4+
5+
import os
6+
from PIL import Image
7+
8+
def resize_photo(source_dir,width,higth,destination_dir):
9+
photos = os.listdir(source_dir)
10+
for photo in photos:
11+
photo_abspath = os.path.join(source_dir,photo)#if you use os.path.abspath,there may be some error
12+
print photo_abspath
13+
if(os.path.isfile(photo_abspath)):#os.path.isfile need a abspath
14+
im = Image.open(photo_abspath)
15+
#w,h = im.size
16+
new_im = im.resize((width,higth))#note: the resize returns a resized copy of an image , so you need a new object to save it
17+
destination_path = os.path.join(destination_dir,photo)
18+
new_im.save(destination_path)
19+
print destination_path
20+
resize_photo('C:/Users/razzl/Desktop/1',800,800,'C:/Users/razzl/Desktop/2')
21+
22+

0 commit comments

Comments
 (0)