-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfiles.py
100 lines (66 loc) · 2.29 KB
/
files.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#Open how to create a file and write something in the file created
names = input()
file=open("test.txt","a") #a is used to append the content
file.write(f"{names} \n")
file.close
#Open a file which you have saved above and then pass a arg
#once the file is created then open it using the code.
#if we want to add the default arguments
names = []
with open("test.txt") as file:
for i in file:
names.append(i)
for name in (names):
print(f"Hello {name}",end="")
student=[]
with open("student.csv") as file:
for line in file:
name,house=line.rstrip().split(",")
student.append(f"{name} is in {house}")
for i in sorted(student):
print(i)
students=[]
with open("student.csv") as file:
for line in file:
name,city=line.rstrip().split(",")
stnd = {"name": name,"city": city}
students.append(stnd)
for i in stnd:
print(i)
# importing a csv file to read easily when we have a other delimiter in the text
#other ways to print the dta we have
import csv
students=[]
with open("student.csv") as file:
reader=csv.reader(file)
for name,home in reader:
students.append({"name":name,"home":home})
for student in sorted(students,key=lambda student: student ["name"]):
print(f'{student["name"]} is in {student["home"]}')
#Using the DictReader
#this will work even columns are interchanged this is used to read the file
import csv
students=[]
with open("student.csv") as file:
reader=csv.DictReader(file)
for row in reader:
students.append({"name":row["name"],"home":row["home"]})
for student in sorted(students,key=lambda student: student ["name"]):
print(f'{student["name"]} is in {student["home"]}')
#using the DictWriter to update the CSV file
import csv
name=input("input a name: ")
home=input("input a house: ")
with open("student.csv",'a') as file:
writer= csv.DictWriter(file, fieldnames=["name","home"])
writer.writerow({"name":name, "home":home})
#creating a Gif's using "arg"
#import image and take 2 images and combine them with a 200 millisec to make a Gif
import sys
from PIL import Image
images=[]
for arg in sys.argv[1:]:
image=image.open(arg)
images.append(image)
images[0].save(
"costumes.gif", save_all=True, append_images=[image[1]],duration=200, loop=0)