-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcsv_import.py
More file actions
39 lines (31 loc) · 1.11 KB
/
Copy pathcsv_import.py
File metadata and controls
39 lines (31 loc) · 1.11 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
import csv
import os
def csv_import():
#Define the path of csv to import
print('Type the csv path to import')
input_path = input('>> ')
#Verify if the csv exists
if not os.path.isfile(input_path):
print("Csv not found!")
return
#Define the path to import the csv content
print('Type the output path')
output_path = input('>> ')
#Verify if the file already exists, if exists allow to override or abort
if os.path.isfile(output_path):
confirm = input("The file already exist, do you wan't yo override? (y/n): ")
if confirm.lower() != 'y':
print("Canceled!")
return
#Creates the csv file
open(output_path, 'w').close()
#Write the content in csv
try:
with open(input_path, 'r') as input_file, open(output_path, 'w', newline='') as output_file:
csv_reader = csv.reader(input_file)
csv_writer = csv.writer(output_file)
for line in csv_reader:
csv_writer.writerow(line)
print("Ended import!")
except Exception as e:
print("Error:", str(e))