0

I've written a script that takes a .csv file, converts it to an .xlsx using openpyxl, and formats the file.

Throughout the script's process, it creates several .csv files, which can be deleted. I've tried using os.remove("File.csv"), but always get the error:

      os.remove("File.csv")
AttributeError: 'str' object has no attribute 'remove'

I've tried running this on 3 different computers, and I've even written a test script with just 2 lines of code to test the functionality and have had no luck (same error):

import os
os.remove("File.csv")

Does anybody know the cause/reason for this?

OR

Is there another way to delete (move to trash) a file/multiple files?

13
  • that should work. check if the file.csv is in the same path as you main.py Commented Feb 10, 2022 at 3:15
  • 1
    I cannot reproduce your second result. In the second example, I get a FileNotFoundError. What version of python are you running? Commented Feb 10, 2022 at 3:15
  • @Aaseer, it is - I've tried this with multiple files, and I've moved the script and the file to multiple folders with no other files in it. I'm using version 3.9.2 Commented Feb 10, 2022 at 3:23
  • Is there another way to delete files? Commented Feb 10, 2022 at 3:24
  • How, exactly are you running your script? Commented Feb 10, 2022 at 3:32

1 Answer 1

1

Try finding the file using os.path.exists Example:

import os
if os.path.exists("File.csv"):
   os.remove("File.csv")
else:
   print("That file does not exist!")
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.