Python Written Database


#1

Hey everyone,

I recently began to work on a database of text files, accessible by a python file. It has a two python files so far, a login and the actual database handler. The login, if correctly logged into, will access the database handler, and run it. The issue is in the files it is supposed to open. If someone could take a look at the below and let me know the issue, that would be great. Thanks. :slight_smile:


Code:

PythonDatabaseFiles = list("/Users/*********/Desktop/PythonDatabase")



import os
root = os.listdir("/Users/*********/Desktop/PythonDatabase")



#------------------------------------------------------------------------------#
loopvar = True
flist = open('flist.txt','r')
dirs = os.listdir("/Users/brinbrody/Desktop/PythonDatabase/Files")

print("SECURITY PASSED")
print("ENTERING DATABASE")

while (loopvar == True):
    fcho = input("ENTER FILE CHOICE (INCLUDE .TXT AT END), VIEW FILES, OR END: ")

    if (fcho == "view files" or fcho == "VIEW FILES" or fcho == "View File"):
        print (flist.read())

    elif (fcho in dirs):
        print("#Figure out how to read chosen file.")

    elif (fcho == "END" or fcho == "end"):
        break
    
print("DATABASE CLOSING")

Please note that I censored out my name for privacy purposes so that is not the issue.
Thanks for the help!


Edit

The following is the issue:

It does not throw an error, however, it ignores the request for a file and continues the loop.


#2

This is an example of where case switching is useful.

if fcho.lower() == "view files":

#3

This is not a valid directory (not on any os that I know). If you're using windows, It's probably C://Users/*********/Desktop/PythonDatabase


#4

@nicoekkart Thanks for the try, but I'm working on a Mac for this specific project, and that is listed as the correct directory for the files:

As I stated in the original post I have the login file and the database handler. The database handler is in the same folder and that filepath routes correctly from that file, so it might be another error in my code.


#5

@mtf
Out of curiosity, you removed the parenthesis around the if statement...

I learned that if/elif needs them, is that a possible source of an error, or just a small syntax change that python will accept either way?


#6

If you're using python2.x, you should use raw_input to not see the spaces as seperate inputs. This solves the error it's currently getting with me. Is there anything else wrong?


#7

Check the path of the specified file i.e. is it available from the folder where the script is being run? The easiest way to check is below:

import os
print (os.getcwd())   #Python 3

print os.getcwd()   #Python 2

#8

@nicoekkart I'm running 3.5, so that didn't fix.

@allentv As I said before in this post, that is not the issue.


#9

You have mentioned that the issue is with the file. Can you share any of the exception error messages? It is very hard to help as the environment you have is not replicable.


#10

@allentv Certainly. I will make an edit to my original post in a few minutes with the exception.


#11

It works perfectly on my machine (linux). The only thing that I see can possibly be wrong is the folder locations. How do you know for sure this is not the issue?


#12

@nicoekkart
As I said, the other python program, using the same path, has no problem.


#13

This is the code I'm running:

PythonDatabaseFiles = list("./PythonDatabase")



import os
root = os.listdir("./PythonDatabase")



#------------------------------------------------------------------------------#
loopvar = True
flist = open('flist.txt','r')
dirs = os.listdir("./PythonDatabase/Files")

print("SECURITY PASSED")
print("ENTERING DATABASE")

while (loopvar == True):
    fcho = input("ENTER FILE CHOICE (INCLUDE .TXT AT END), VIEW FILES, OR END: ")

    if (fcho == "view files" or fcho == "VIEW FILES" or fcho == "View File"):
        print (flist.read())

    elif (fcho in dirs):
        print("#Figure out how to read chosen file.")

    elif (fcho == "END" or fcho == "end"):
        break
    
print("DATABASE CLOSING")

Inside the folder where this code resides, I have the following structure:

. (current folder)
    flist.txt
    PythonDatabase
        Files
            file1
            file2
            file3
            file4
            file5

Try to check if you have the same folder structure. I'm guessing there's something wrong with your Files path. If that's not the problem, try to print some things to debug. Since I'm not having any problems, it's hard to do that.


#14

I will certainly try it. Thanks for the help. I'll post a response if it works and vice versa.