Python Forum
Why do i get these errors?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Why do i get these errors?
#11
I found it.
Sry for disturbing you!!
Reply
#12
I need help on solving the errors,

Script,
import csv
import os.path
import time
import imp
import sys

if not os.path.exists(r"D:\My folder\Projects\Programing\Python\Student Data storing app\Register.csv"):
   #run start up script
   print("Cache was Deleted!\nCreating new Cache")
   time.sleep(3)
   with open (r"D:\My folder\Projects\Programing\Python\Student Data storing app\Register.csv", "w") as new_file :
       fieldnames = ["Full name", "Phone 1", "Phone 2", "Identity Number", "Email", "Paid/not"]
       csv_writer = csv.DictWriter(new_file, fieldnames = fieldnames, delimiter = ",")
       csv_writer.writeheader()

   file = open (r"D:\My folder\Projects\Programing\Python\Student Data storing app\Data.txt", "w") #creates our file to say startup is complete you could write to this if you wanted as well
   def pass_new():
       password = input("Please enter a password.\nUse only letters.")
       pass_len = len(password)
       if pass_len < 10:
           print("Password saved!")
           file.write("Pass = " + password)
       else : print("Password must not exceed 10 characters.")
       sys.exit()


   pass_new()

   file.write()
   print("Startup is complete.")


   file.close

def pass_check():
    f = open(r"D:\My folder\Projects\Programing\Python\Student Data storing app\Data.txt")
    global data
    data = imp.load_source('data', '', f)
    f.close()
    correct_pass = data.Pass
    user_input_pass = input("Please enter your Password.")
    if user_input_pass == correct_pass:
        print("Password accepted!")
    else : print("Password incorrect.\nAccess denied!")
    sys.exit()

pass_check()
print("'add' - Adds a new user. \n'update' - Updates a user.")
print("'remove' - Removes a user.\n {confrim}")

user_input = input("Enter a command : ").lower()

if user_input == "add":
    print("Command - 'add'\n{Fullname} {Phone 1} {Phone 2} {Identity Number} {Email} {Paid/not}")
else : print("Error!\nPlease enter a valid option.")

if user_input == "update":
    print("Command - 'update'\n{Fullname} {Phone 1} {Phone 2} {Identity Number} {Email} {Paid/not}")
else : print("Error!\nPlease enter a valid option.")


if user_input == "remove":
    print("Command - 'remove'\n {Full name} {Identity number} ")
    confirm = input("Please 'Confirm' removal of user.\nThis cannot be undone! ~~~ (Type 'confirm')").lower()
    if confirm == "confrim":
        print("User removed")
    else : print("User not removed. :D")
else : print("Error!\nPlease enter a valid option.")
Errors,
Error:
C:\Users\User\AppData\Local\Temp\atom_script_tempfiles\9967ca60-3fa2-11eb-8856-c9174d06c43f:72: DeprecationWarning: the imp module is deprecated in favour of importlib; see the module's documentation for alternative uses import imp Traceback (most recent call last): File "C:\Users\User\AppData\Local\Temp\atom_script_tempfiles\9967ca60-3fa2-11eb-8856-c9174d06c43f", line 115, in <module> pass_check() File "C:\Users\User\AppData\Local\Temp\atom_script_tempfiles\9967ca60-3fa2-11eb-8856-c9174d06c43f", line 106, in pass_check data = imp.load_source('data', '', f) File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\imp.py", line 171, in load_source module = _load(spec) File "<frozen importlib._bootstrap>", line 711, in _load File "<frozen importlib._bootstrap>", line 680, in _load_unlocked File "<frozen importlib._bootstrap_external>", line 786, in exec_module File "<frozen importlib._bootstrap_external>", line 922, in get_code File "C:\Users\User\AppData\Local\Programs\Python\Python39\lib\imp.py", line 152, in get_data self.file = file = open(self.path, 'rb') FileNotFoundError: [Errno 2] No such file or directory: ''
What causes these errors?
I don't know how to fix it!.
Reply
#13
Please, don't start new thread. Keep the discussion in this thread. If the answer you get does not solve the problem, say so, don't start new thread.

what exactly you want to do with imp? You don't need it at all.
also what do you think this line data = imp.load_source('data', '', f) is doing?

I would suggest you read again on how you work with files (like csv files) in python. here is our tutorial: https://python-forum.io/Thread-Basic-Files

Note that there are number of issues with your script (that don't raise errors [yet]). For example:
  • on lines 11-14 you create a csv file, then you completely overwrite it on line 16.
  • use of global variables should be avoided
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#14
(Dec-16-2020, 02:23 PM)buran Wrote: Please, don't start new thread. Keep the discussion in this thread. If the answer you get does not solve the problem, say so, don't start new thread.
Sorry, but it solved some of the problems!.
I'll not do it again.

(Dec-16-2020, 02:23 PM)buran Wrote: what exactly you want to do with imp? You don't need it at all.
That is the thing. i was watching a tutorial.
I don't know what it does, but I haven't finished the script though.
If you want. let me know, ill link the tutorial.

(Dec-16-2020, 02:23 PM)buran Wrote: also what do you think this line data = imp.load_source('data', '', f) is doing?
That also I don't know.

(Dec-16-2020, 02:23 PM)buran Wrote: on lines 11-14, you create a CSV file, then you completely overwrite it on line 16.
No, I create register.csv in the lines 11 - 14.
and create another file, data.txt in line 16.
Doesn't it?
Reply
#15
(Dec-16-2020, 02:56 PM)Oshadha Wrote: No, I create register.csv in the lines 11 - 14.
and create another file, data.txt in line 16.
Doesn't it?
Yes, sorry, my error - with these long path names.

the imp (now depreciated and replaced with importlib) is used to import modules using module name. You don't need to use it in simple script like yours. In any case it is not intended to "load" data/csv/txt files. Open the csv/txt file in read mode (i.e. r). You may decide to use csv module to read it - it will make it more convenient, but not necessary. You can just read the file with e.g. read() or readlines() method.

Also, it's better to use with context manager, like on line 11. Use it also when creating data.txt
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#16
As a side note - if using csv/txt is not mandatory, you may look into using different format like JSON or even better - a real detabase like sqlite3 (support for sqlite3, which is file based database comes with python).
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#17
(Dec-16-2020, 03:12 PM)buran Wrote: As a side note - if using csv/txt is not mandatory, you may look into using different format like JSON or even better - a real detabase like sqlite3 (support for sqlite3, which is file based database comes with python).
Lets put my script aside.
Now, I want to make a system. that stores data about students.
I'm storing data in a csv file.
I want to know all the commands when working with csv.(I want them clearly pls)
And all the commands when working with txt files.(Also clearly)

If you can link me these commands then,
I can script a new one without watching a tutorial. (So I can understand better!)

Then I want to know how to turn .py into .exe.
So that people cant edit the script/see the script.
Reply
#18
There is a module in the standard library for working with CSV files: https://docs.python.org/3/library/csv.html. You really should familiarise yourself with the standard library docs if you haven't already.

Also, why do you want to stop people reading your script? Who'll be using it anyway? As for stopping edits, can't you just use whatever your OS has for managing permissions? Looks like you're on Windows, so I can't help further with that.
Oshadha likes this post
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020