Quantcast
Channel: Data Science, Analytics and Big Data discussions - Latest topics
Viewing all articles
Browse latest Browse all 4448

How to close a file in python

$
0
0

@sachin123456 wrote:

Hi here is my code -
I am writing to csv and when i ran getting the below error

def CSVCreate(file_name):
now = datetime.datetime.now()
my_file = Path(“C:\Users\SJainOF2973\Desktop\LM_Girish\day19_11\Processsed_CSV.csv”)

dfr=pd.read_excel(file_name,sheet_name='CHANGE LOG',usecols=(0,1))
dfr['Updated_Date']=dfr['Date']
dfr=dfr.drop(['Date'],axis=1)
dfr['Processed_Date']=now
dfr['File_Name']=file_name

if not os.path.isfile('my_file'):
    dfr.to_csv(my_file, header='column_names')
          
else:
    dfr.to_csv(my_file, mode='a', header=False) 
my_file.close()    

CSVCreate(file_name)

Error:
AttributeError: ‘WindowsPath’ object has no attribute ‘close’

have tried providing my_file.close at the end but no luck.
Any leads

Thanks in Advance.
Sachin

Posts: 1

Participants: 1

Read full topic


Viewing all articles
Browse latest Browse all 4448

Trending Articles