CunnyFunt
CunnyFunt

Reputation: 105

Python - os.remove() - str object has no attribute 'remove'

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?

Upvotes: 0

Views: 887

Answers (1)

Aaseer
Aaseer

Reputation: 127

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!")

Upvotes: 1

Related Questions