0

I have a chunk of Python code that is supposed to create MySQL schemas when executed. The issue is that whenever I run it, I get an error saying Raise Exception(e). Exception: [Error 2] the system cannot find the file specified

Below is my code:

from test.db.db import DB
from test.config import loggingSetup
loggingSetup.initializeLogging("createTest.py")
import logging

def createSports(db):
   sqlFile = "..\\sql\\createSchema.sql"

   db.run(sqlFile)

def create():
   db=DB()

   createSports(db)

def Main():
    create()

if__name__=='__main__':
   try:
      main()
   except Exception, e:
      logging.info(e.message)
      raise Exception(e)

Now I will admit, this code isn't entirely mine. I found this online, and I'm trying to rework it so it will fit my needs. My experience with python is minimal at best, but you have to start somewhere. On a basic level I under stand that the if statement is saying if __name__=='__main__' then try main(), but i guess I'm fuzzy as to why this exception is being thrown. I know this chunk of code has worked for others who have used it for similar projects, is there something wrong with my syntax that is throwing this off?

Jcmoney1010
  • 912
  • 7
  • 18
  • 41

3 Answers3

0
def createSports(db):
   sqlFile = "..\\sql\\createSchema.sql" #file path goes here

in here you have to write your file path. It's an example. Complete the path and make sure this file is exist.

GLHF
  • 3,835
  • 10
  • 38
  • 83
  • 1
    I added the full path to the createSchema.sql file. This changed the above line to `sqlFile = "C:\User\Desktop\Test\sql\createSchema.sql"` but unfortunately I'm getting the same error as before. – Jcmoney1010 Feb 15 '15 at 04:39
  • You didn't double up the backslashes (or use a raw string). – kindall Feb 15 '15 at 05:32
0

You should know the difference between "working directory" and actually directory. During the runtime, if you don't use the absolute path, the Code(not only python, but other language such as C++, Java), will treat the path as relative path. so the question is what it is relative to ? the answer is "working directory", you can change your working directory easily by "os.chdir". In this case, you need to transfer it to the absolute path:

solution:

1)

def createSports(db):
    sqlFile = "..\\sql\\createSchema.sql"
    sqlFile = os.path.abspath(sqlFile)
    if not os.path.exists(sqlFile):
        msg = "file %s doesn't exist" % sqlFile
        raise Exception(msg)
    else:
       db.run(sqlFile)

2) use the full path, but pay attention to the escaped character, you should use this sqlFile = r"C:\User\Desktop\Test\sql\createSchema.sql", use the "r" to mean that this is raw sting, do NOT escape "\"

Cui Heng
  • 1,265
  • 8
  • 10
  • 1
    hmm, solution one gives me an `unexpected indent` error on all the new lines added. Solution 2 gives me the same error I started with. This is driving me crazy! thanks for your help though! – Jcmoney1010 Feb 15 '15 at 04:59
  • r"C:\User\Desktop\Test\sql\createSchema.sql" if this doesn't work, then you should check if you have enough privileges to that file. – Cui Heng Feb 15 '15 at 05:31
0

Try this :

initialize_logging(__file__) 

It's because the file which you pass to the logging function isn't found. file in short gives you the path to the currently loaded module.

Read more about file here.

Community
  • 1
  • 1
Shay Nehmad
  • 1,103
  • 1
  • 12
  • 25