Jump to content

Python can someone please fix this

oliverjrose99

please can someone fix this, i cant think of how to do it (it's probably really obvious
)

import sysimport easygui as eglist = []def addtolist():    list.insert(0, directory)    print(list)msg = "Click Me"title = "Click Me"eg.msgbox(msg, title)directory = eg.fileopenbox(command = addtolist())

i get this error

Traceback (most recent call last):  File "C:/Users/Oliver Rose/Documents/Python/GUI/test.py", line 14, in <module>    directory = eg.fileopenbox(command = addtolist())  File "C:/Users/Oliver Rose/Documents/Python/GUI/test.py", line 7, in addtolist    list.insert(0, directory)NameError: global name 'directory' is not defined
Link to comment
Share on other sites

Link to post
Share on other sites

You haven't defined directory before you execute this statement

list.insert(0, directory)

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

i fixed it but if i just print directory it might say "C:\Users\Oliver Rose\Documents\Python\GUI\mygui.py" but when i add it to the list it will print "C:\\Users\\Oliver Rose\\Documents\\Python\\GUI\\mygui.py". how can i fix this

Link to comment
Share on other sites

Link to post
Share on other sites

i fixed it but if i just print directory it might say "C:\Users\Oliver Rose\Documents\Python\GUI\mygui.py" but when i add it to the list it will print "C:\\Users\\Oliver Rose\\Documents\\Python\\GUI\\mygui.py". how can i fix this

 

Replace your print statement with

import osprint [item.replace(os.sep, '/') for item in list]
Link to comment
Share on other sites

Link to post
Share on other sites

 

Replace your print statement with

import osprint [item.replace(os.sep, '/') for item in list]

A better way would be to use the raw string modifier.

 

e.g:

 

r"C:\Users\Oliver Rose\Documents\Python\GUI\mygui.py"

 

This will make it become the form that the os module uses. Make your code conform to existing Python modules, not the other way around.

I do not feel obliged to believe that the same God who has endowed us with sense, reason and intellect has intended us to forgo their use, and by some other means to give us knowledge which we can attain by them. - Galileo Galilei
Build Logs: Tophat (in progress), DNAF | Useful Links: How To: Choosing Your Storage Devices and Configuration, Case Study: RAID Tolerance to Failure, Reducing Single Points of Failure in Redundant Storage , Why Choose an SSD?, ZFS From A to Z (Eric1024), Advanced RAID: Survival Rates, Flashing LSI RAID Cards (alpenwasser), SAN and Storage Networking

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×