File tree Expand file tree Collapse file tree 2 files changed +30
-23
lines changed Expand file tree Collapse file tree 2 files changed +30
-23
lines changed Original file line number Diff line number Diff line change 19
19
if sys .version_info >= (3 , 0 ):
20
20
raw_input = input
21
21
22
- try_count = 16
23
-
24
- while try_count :
25
- file_name = raw_input ("Enter a file name: " ) # pick a file you have
26
- fhand = open (file_name )
27
- count = 0
28
- for lines in fhand :
29
- count = count + 1
30
- fhand = open (file_name )
31
- inp = fhand .read ()
32
- t_char = len (inp )
33
- try_count >>= 1
34
- try :
35
- file_stats = os .stat (file_name )
36
- print ("This is os.stat" ,file_stats )
37
- break
38
- except OSError :
39
- print ("\n NameError : [%s] No such file or directory\n " , file_name )
40
-
41
- if try_count == 0 :
42
- print ("Trial limit exceeded \n Exiting program" )
22
+ file_name = raw_input ("Enter a file name: " ) # pick a file you have
23
+ count = 0
24
+ t_char = 0
25
+ try :
26
+ with open (file_name ) as f :
27
+ line = f .readline ()
28
+ t_char += len (line )
29
+ while line :
30
+ count += 1
31
+ line = f .readline ()
32
+ t_char += len (line )
33
+ except FileNotFoundError as e :
34
+ print (e )
43
35
sys .exit ()
44
-
36
+
37
+ file_stats = os .stat (file_name )
45
38
# create a dictionary to hold file info
46
39
file_info = {
47
40
'fname' : file_name ,
Original file line number Diff line number Diff line change @@ -16,7 +16,21 @@ def download(url):
16
16
f .close ()
17
17
print ("Succesfully Downloaded" )
18
18
19
+ #Function is do same thing as method(download) do,but more strict
20
+ def download_2 (url ):
21
+ try :
22
+ response = requests .get (url )
23
+ except Exception :
24
+ print ('Failed Download!' )
25
+ else :
26
+ if response .status_code == 200 :
27
+ with open ('file_name.jpg' ,'wb' ) as f :
28
+ f .write (requests .get (url ).content )
29
+ print ("Succesfully Downloaded" )
30
+ else :
31
+ print ('Failed Download!' )
32
+
19
33
url = 'https://avatars0.githubusercontent.com/u/29729380?s=400&v=4' #URL from which we want to download
20
-
34
+
21
35
download (url )
22
36
You can’t perform that action at this time.
0 commit comments