1+ import sqlite3 as lite #importing sqlite (database)
2+ import sys #importing module that gives access to computer system
3+ import os , shutil #importing os module that allows us to use operating system dependent functionality
4+ #importing shutil that allows file copying and/or removing.
5+ from datetime import datetime #importing module enabling us to see the date and time.
6+ import os .path #importing pathname manipulations
7+ import time #importing time module so we can make the programme sleep for a certain amount of secs.
8+
9+ # Create a version of the database in database file (.db)
10+ DB_NAME = "mydatabase.db"
11+ con = lite .connect (DB_NAME )
12+ #creating a datetime variable, with timezone = none.
13+ datetime .now (tz = None )
14+
15+ #defining a function with variables that allow us create variables
16+ def dbFileInsert (tableName , fileName , filePath ):
17+ # Creates the SQLite cursor that is used to query the database
18+ cur = con .cursor ()
19+ #Executing the desired database script
20+ cur .executescript ("""
21+ DROP TABLE IF EXISTS """ + tableName + """;
22+ CREATE TABLE """ + tableName + """ (fileName TEXT, filePath TEXT);
23+ INSERT INTO """ + tableName + """ VALUES('""" + fileName + """','""" + filePath + """');
24+ """ )
25+
26+
27+ # Force the database to make changes with the commit command
28+ con .commit ()
29+
30+ # Execute simple SQL query
31+ cur .execute ('SELECT * FROM ' + tableName )
32+ for i in cur :
33+ print ("\n " )
34+ for j in i :
35+ print (j )
36+
37+
38+ # Close the database
39+ con .close ()
40+
41+
42+ def fileCopy (fileName , fileFrom , fileTo ):
43+ os .chdir ('C:\\ ' )
44+ print ("Changing directionary to the C-drive..." )
45+ os .path .isfile (fileTo )
46+ os .path .isfile (fileTo + "\\ " + fileName )
47+
48+ # attempting to create a new folder.. if the folder already exists, the programme proceeds to execute the next step
49+ if os .path .exists (fileTo ) == False :
50+ os .system ('mkdir NyFiskeFolder' )
51+ print ("Creating new folder..." )
52+ elif os .path .exists (fileTo ) == True :
53+ print ("The folder already exists. Trying to copy image..." )
54+ time .sleep (2 )
55+
56+ #copying image from old folder to newly created folder and prints the time, unless already exists.
57+ if os .path .exists (fileTo + "\\ " + fileName ) == False :
58+ shutil .copy (fileFrom + "\\ " + fileName ,fileTo + "\\ " + fileName )
59+ shutil .copy (fileFrom + "\\ " + fileName ,fileTo + "\\ " + fileName )
60+ dateTimeObj = datetime .now ()
61+ print ("Image has been copied to new folder at this time: " , dateTimeObj )
62+ elif os .path .exists (fileTo + "\\ " + fileName ) == True :
63+ print ("The image has already been copied!" )
64+
65+
66+ #calling the functions with the desired variables
67+ dbFileInsert ("Images" , r"fisk1.jpg" , r"\content\images" )
68+
69+ fileCopy ("fisk1.jpg" ,r"C:\\FiskeFolder" ,r"C:\\NyFiskeFolder" )
0 commit comments