Search code examples
pythondataframecsvexport-to-csv

Is there any way to make df.to_csv in Read Only mode?


clients.to_csv('Machine Testing Database.csv',index=False)

I need the end result csv file in Read only mode, is there any way?


Solution

  • "df.to_csv" cannot do this, but you can use "os.chmod" to change the file's mode to "readonly" after do "df.to_csv".

    you can do it like:

    import os
    import stat
    import pandas as pd
    
    df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                       'mask': ['red', 'purple'],
                       'weapon': ['sai', 'bo staff']})
    filepath = 'test.txt'
    df.to_csv(filepath, index=None)
    os.chmod(filepath, stat.S_IREAD) # change the file's mode to readonly