Home>

Returned at code execution
PermissionError: [Errno 13] Permission denied
Eliminate
I want to save data in the specified directory.

environment

Python Version: 3.7.6
Execution environment: Jupyterlab on Windows 10

Current status/trial

I ran the following code #A
PermissionError: [Errno 13] Permission denied
Was returned.

The error content at that time is #B.
I ran #C to check the directory permissions, but they were all true.
After that, after granting the authority as #D, the same error occurred even if #A was executed again.

I want to solve this and save the file.

#A
import os, datetime
import pandas as pd
from datetime import datetime
now = datetime.now ()
dt = now.timestamp ()
os.mkdir (r'C: \ Users \ user \ Desktop \ {}'. format (dt))
test = ['a','b','c']
test2 = pd.Series (test)
test2.to_csv (r'C: \ Users \ user \ Desktop \ {}'. format (dt))
#B
PermissionError Traceback (most recent call last)
<ipython-input-8-735ba69dc4ab>in<module>
      3
      4 test2 = pd.Series (test)
---->5 test2.to_csv (r'C: \ Users \ user \ Desktop \ {}'. format (dt))
      6
      7
~ \ anaconda3 \ lib \ site-packages \ pandas \ core \ generic.py in to_csv (self, path_or_buf, sep, na_rep, float_format, columns, header, index, index_label, mode, encoding, compression, quoting, quotechar, line_terminator, chunksize, date_format, doublequote, escapechar, decimal)
   3202 decimal = decimal,
   3203)
->3204 formatter.save ()
   3205
   3206 if path_or_buf is None:
~ \ anaconda3 \ lib \ site-packages \ pandas \ io \ formats \ csvs.py in save (self)
    186 self.mode,
    187 encoding = self.encoding,
->188 compression = dict (self.compression_args, method = self.compression),
    189)
    190 close = True
~ \ anaconda3 \ lib \ site-packages \ pandas \ io \ common.py in get_handle (path_or_buf, mode, encoding, compression, memory_map, is_text)
    426 if encoding:
    427 # Encoding
->428 f = open (path_or_buf, mode, encoding = encoding, newline = "")
    429 elif is_text:
    430 # No explicit encoding
PermissionError: [Errno 13] Permission denied:'C: \\ Users \\ user \\ Desktop \\ 1605666288.395685'
#C
print (os.access (r'C: \ Users \ user \ Desktop \ {}'. format (dt), os.F_OK))
print (os.access (r'C: \ Users \ user \ Desktop \ {}'. format (dt), os.R_OK))
print (os.access (r'C: \ Users \ user \ Desktop \ {}'. format (dt), os.W_OK))
print (os.access (r'C: \ Users \ user \ Desktop \ {}'. format (dt), os.X_OK))
print (os.access (r'C: \ Users \ user', os.F_OK))
print (os.access (r'C: \ Users \ user', os.R_OK))
print (os.access (r'C: \ Users \ user', os.W_OK))
print (os.access (r'C: \ Users \ user', os.X_OK))
#D
os.chdir (r'C: \ Users \ user \ Desktop \ {}'. format (dt))
os.chmod (r'C: \ Users \ user \ Desktop \ {}'. format (dt), 0o777)

Thanks for your cooperation.

  • Answer # 1

    What you should pass to to_csv is the file path you want to create, not the directory path.
    test2.to_csv (r'C: \ Users \ user \ Desktop \ {} \ hoge.csv'.format (dt))File name likehoge.csvPlease specify.

  • Answer # 2

    PermissionError: [Errno 13] Permission denied:'C: \\ Users \\ user \\ Desktop \\ 1605666288.395685'

    The quick story is that the file has failed to save.
    A simple file write does not even create a directory, so
    First of all, please check if the directory C: \ Users \ user \ Desktop exists.