File tree Expand file tree Collapse file tree 2 files changed +37
-0
lines changed
Expand file tree Collapse file tree 2 files changed +37
-0
lines changed Original file line number Diff line number Diff line change 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
Original file line number Diff line number Diff line change 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+
You can’t perform that action at this time.
0 commit comments