-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathprogram.py
68 lines (48 loc) · 2.02 KB
/
program.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
# Import modules
import dbfread
import subprocess as sub
import pandas as pd
import os
# Define functions
def clear():
"""Clear the console screen on windows"""
sub.call('cls', shell=True)
def main():
"""Main function to convert all DBF files into CSV files"""
# Declaration
cnt_errors = 0
# Get the script path, all DBF files inside this path will be converted into CSV.
script_path = os.path.dirname(__file__)
# Clear the console screen
clear()
# Script is starting to find all DBF files.
print('Script is searching for DBF files.')
# Search for DBF files inside the script path.
for dirpath, dirname, filenames in os.walk(script_path):
for filename in filenames:
if filename.endswith(".dbf"):
print("Convert: {filename} to .csv".format(filename=filename))
# Combine both strings
full_path = dirpath + "\\" + filename
# Try to load the DBF file
try:
table = dbfread.DBF(full_path, encoding="windows-1252", ignore_missing_memofile=False)
except dbfread.exceptions.DBFNotFound as dbf_exc:
print("Error occurred: \n{file} \n{error}".format(file=filename, error=dbf_exc))
cnt_errors += 1
continue
# Load data from table into an DataFrame.
df = pd.DataFrame(iter(table))
# Remove last four characters.
csv_file = filename[:-4] + ".csv"
# Join the script path.
output_path_csv = os.path.join(script_path, csv_file)
# Print a message and create the csv file.
print("Convert: {filename} to .csv".format(filename=filename))
df.to_csv(output_path_csv, sep=';')
# Print out amount of not converted DBF files.
if cnt_errors > 0:
print('Amount of not converted files: {}'.format(cnt_errors))
# Application entry point
if __name__ == '__main__':
main()