Python Forum
Saving a file to a specific location
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Saving a file to a specific location
#11
(Apr-23-2020, 02:17 PM)finndude Wrote: i dont understand theres nothing else to delete it only that code.

It's a design pattern. Maybe it's not relevant for this particular case, but you should be aware of potential problems and what is accepted solution. Think of multiple users running your code, your more advanced code running multi-thread or multi-process, etc.

what is the problem with os.remove?
Also, note that opening file for writing (in 'w' mode) will overwrite file if it exists. No need to remove it before that.
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
#12
basically all the files will be in the user info folder. but the program is saved in a different file if I use os remove on its own it's gonna try and delete a file in the folder the code is, giving an error because its not there.

so I want it to remove a file from the user info folder.
with the path exist. i want it to check if the file exists in the user info folder
"Only Boring People Get Bored"
Reply
#13
you need to pass the full path to file you want to delete
import os

user = 'delete-this_user'
fname = f'{user}.txt'
folder = 'E:/Work/School/Computer Science/Python/Code/User Info/'
try:
    os.remove(os.path.join(folder, fname))
except FileNotFoundError:
    # file does not exists
    pass
Again, note that if you are going to write again, it will be overwritten anyway
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
alright is there a way so that it looks in the folder user info and then delete the file that the user inputted.

i have a system in place using path.exist to see if its there. if it's not there it prints that it's not

whats the simple way to look in a folder and delete the chosen file

also how do I use path.exists to look in a chosen folder aswell
"Only Boring People Get Bored"
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 500 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Reading Specific Rows In a CSV File finndude 3 973 Dec-13-2022, 03:19 PM
Last Post: finndude
  Saving the times a script is run to a file or ... 3Pinter 7 1,387 Oct-19-2022, 05:38 PM
Last Post: 3Pinter
  Code Assistance needed in saving the file MithunT 0 811 Oct-09-2022, 03:50 PM
Last Post: MithunT
  Changing file location azizrasul 6 1,284 Sep-28-2022, 01:01 AM
Last Post: azizrasul
  Saving the print result in a text file Calli 8 1,783 Sep-25-2022, 06:38 PM
Last Post: snippsat
  How to extract specific data from .SRC (note pad file) Shinny_Shin 2 1,262 Jul-27-2022, 12:31 PM
Last Post: Larz60+
  Extracting Specific Lines from text file based on content. jokerfmj 8 2,952 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Trying to determine attachment file type before saving off.. cubangt 1 2,139 Feb-23-2022, 07:45 PM
Last Post: cubangt
  Showing and saving the output of a python file run through bash Rim 3 2,424 Oct-06-2021, 10:48 AM
Last Post: gerpark

Forum Jump:

User Panel Messages

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