-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFile_Handling.py
More file actions
91 lines (58 loc) · 2.4 KB
/
Copy pathFile_Handling.py
File metadata and controls
91 lines (58 loc) · 2.4 KB
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
# File Handling
import os
# .txt file
"""
# Syntax
open('filename', mode) # mode(r, a, w, x, t,b) could be to read, write, update
"r" - Read - Default value. Opens a file for reading, it returns an error if the file does not exist
"a" - Append - Opens a file for appending, creates the file if it does not exist
"w" - Write - Opens a file for writing, creates the file if it does not exist
"x" - Create - Creates the specified file, returns an error if the file exists
"t" - Text - Default value. Text mode
"b" - Binary - Binary mode (e.g. images)
"""
txt_file = open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.txt", "w+")
txt_file.write("Mi nombre es Joel\nMi apellido es Stadelman\nTengo 23 años\nMi lenguaje preferido es Python")
print(txt_file.read())
print(txt_file.read(10))
print(txt_file.readline())
print(txt_file.readline())
#print(txt_file.readlines()) # esta funcion itera el fichero y lo convierte en una lista.
for line in txt_file.readlines():
print(line)
txt_file.write("\nAunque tambien me gusta C#")
print(txt_file.readline())
txt_file.close()
#os.remove("D:\Programas\VS Code\VS Code Projects\Python-Projects\my_file.txt")
# .json file
import json
json_file = open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.json", "w+")
json_test = {
"name":"Joel",
"surname":"Stadelman",
"age":23,
"languages":["Python", "MongoDB", "Kotlin"],
"website":"https://joelst.dev"}
json.dump(json_test, json_file, indent=2)
json_file.close()
with open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.json") as my_other_file:
for line in my_other_file.readlines():
print(line)
json_dict = json.load(open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.json"))
print(json_dict)
print(type(json_dict))
# .csv file
import csv
csv_file = open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.csv", "w+")
csv_writer = csv.writer(csv_file)
csv_writer.writerow(["name", "surname","age" ,"language","website"])
csv_writer.writerow(["Joel", "Stadelman", 23, "Python","https://joelst.dev"])
csv_writer.writerow(["Roswell", "", 2, "COBOL",""])
csv_file.close()
with open("D:/Programas/VS Code/VS Code Projects/Python-Projects/my_file.csv") as my_other_file:
for line in my_other_file.readlines():
print(line)
# .xlsx
# import xlrd # debe instalarse el modulo
# .xml file
import xml