Python Forum

Full Version: Saving a file to a specific location
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2
Hi,

Im using this code to create a file with the username as the title and the password inside the file.

user_details = open(new_username_1 + ".txt","w") #creates a new file called "username".txt
user_details.write(new_password_1) #writes in the file what the users password is
user_details.close() #saves and closes the file
how what I get this to save to a specific folder that I have created

atm it saves in:

E:\Work\School\Computer Science\Python\Code

I want it to save in:

E:\Work\School\Computer Science\Python\Code\User Info

thanks
user_details = open('E:\Work\School\Computer Science\Python\Code\User Info\' + new_username_1 + ".txt","w") 
cheers mate
it's strange how using single backslash (not escaped, not in a raw string) is not giving you an error for e.g. \User part of the path.
>>> '\User'
  File "<stdin>", line 1
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \UXXXXXXXX escape
>>> 
'\u' is one of the escape sequences...
when i put your code in the whole thing stays as a string is this how its supposed to be?

it is giving me an error

[error
EOL while scanning literal
[/error]
when working with paths in Windows where separator is backslash (normally) you have three alternatives:
escaping the backslash:
folder = 'E:\\Work\School\\Computer Science\\Python\\Code\\User Info\\'
using raw string
folder = r'E:\Work\\School\Computer Science\Python\Code\User Info\'
or using forward slash
folder = 'E:/Work/School/Computer Science/Python/Code/User Info/'
using single backslash will cause error if backlash is followed by certain chars that in combination with backslash represent escape sequence. In your case '\u is such combination.
thanks im going to try it with single forward slashes first

how would I implement this ti when checking for a path e.g

username_taken_check_1 = path.exists(new_username_1 + ".txt")
I want to check in the user info folder

also how would i implement it with an os remove:

os.remove(delete_user_2 + ".txt")
again in the user info folder
i have these sections of code here:


delete_account_check_1 = path.exists(delete_user_1 + ".txt") #checks if the path of "username".txt exists
and

os.remove(delete_user_1 + ".txt") #deletes the file/account
I want to check in the file path 'E:/Work/School/Computer Science/Python/Code/User Info/' (I have used forward slash)

I am checking to see if it exists in the first bit of code

in the second bit, I want to delete the file from the path that I looked in previously (E:/Work/School/Computer Science/Python/Code/User Info/)

thanks
what you suggest is prone to race condition - i.e. the file may exists when you perform the check and then get deleted by another process between check and your attempt to delete it.
so, without check, wrap the code that delete the file in try/except and catch the error if file does not exists in the first place
i dont understand theres nothing else to delete it only that code.

what would the code be to delete a file from E:/Work/School/Computer Science/Python/Code/User Info/
Pages: 1 2