Skip to content

Commit 18241e6

Browse files
Merge pull request geekcomputers#518 from Hong199/master
Update fileinfo.py
2 parents aa28162 + 86134a2 commit 18241e6

File tree

2 files changed

+30
-23
lines changed

2 files changed

+30
-23
lines changed

fileinfo.py

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,22 @@
1919
if sys.version_info >= (3, 0):
2020
raw_input = input
2121

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 ("\nNameError : [%s] No such file or directory\n", file_name)
40-
41-
if try_count == 0:
42-
print ("Trial limit exceeded \nExiting 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)
4335
sys.exit()
44-
36+
37+
file_stats = os.stat(file_name)
4538
# create a dictionary to hold file info
4639
file_info = {
4740
'fname': file_name,

scrap_file.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,21 @@ def download(url):
1616
f.close()
1717
print("Succesfully Downloaded")
1818

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+
1933
url='https://avatars0.githubusercontent.com/u/29729380?s=400&v=4' #URL from which we want to download
20-
34+
2135
download(url)
2236

0 commit comments

Comments
 (0)