我正试图遍历csv文件的文件夹,但被PermissionError阻止,奇怪的是,当我在windows 10机器上使用管理员帐户登录时,文件夹应该是可以访问的,而文件则在一个新文件夹中,安全设置为可以读写。我担心这是一个愚蠢的窗口权限问题,我花了很多小时试图解决这个问题:(。
import pandas as pd
import os
raw_data_path = 'C:\Users\jeppe\data\'
filenames = os.listdir(raw_data_path)
for file in filenames:
df = pd.read_csv(f'{raw_data_path}{file}')
..append some code
PermissionError Traceback (most recent call last)
~AppDataLocalTemp/ipykernel_12388/436724791.py in <module>
----> 1 pd.read_csv(Path(f'{raw_data_path}{file}'))
~anaconda3libsite-packagespandasutil_decorators.py in wrapper(*args, **kwargs)
309 stacklevel=stacklevel,
310 )
--> 311 return func(*args, **kwargs)
312
313 return wrapper
~anaconda3libsite-packagespandasioparsersreaders.py in read_csv(filepath_or_buffer, sep, delimiter, header, names, index_col, usecols, squeeze, prefix, mangle_dupe_cols, dtype, engine, converters, true_values, false_values, skipinitialspace, skiprows, skipfooter, nrows, na_values, keep_default_na, na_filter, verbose, skip_blank_lines, parse_dates, infer_datetime_format, keep_date_col, date_parser, dayfirst, cache_dates, iterator, chunksize, compression, thousands, decimal, lineterminator, quotechar, quoting, doublequote, escapechar, comment, encoding, encoding_errors, dialect, error_bad_lines, warn_bad_lines, on_bad_lines, delim_whitespace, low_memory, memory_map, float_precision, storage_options)
584 kwds.update(kwds_defaults)
585
--> 586 return _read(filepath_or_buffer, kwds)
587
588
~anaconda3libsite-packagespandasioparsersreaders.py in _read(filepath_or_buffer, kwds)
480
481 # Create the parser.
--> 482 parser = TextFileReader(filepath_or_buffer, **kwds)
483
484 if chunksize or iterator:
~anaconda3libsite-packagespandasioparsersreaders.py in __init__(self, f, engine, **kwds)
809 self.options["has_index_names"] = kwds["has_index_names"]
810
--> 811 self._engine = self._make_engine(self.engine)
812
813 def close(self):
~anaconda3libsite-packagespandasioparsersreaders.py in _make_engine(self, engine)
1038 )
1039 # error: Too many arguments for "ParserBase"
-> 1040 return mapping[engine](self.f, **self.options) # type: ignore[call-arg]
1041
1042 def _failover_to_python(self):
~anaconda3libsite-packagespandasioparsersc_parser_wrapper.py in __init__(self, src, **kwds)
49
50 # open handles
---> 51 self._open_handles(src, kwds)
52 assert self.handles is not None
53
~anaconda3libsite-packagespandasioparsersbase_parser.py in _open_handles(self, src, kwds)
220 Let the readers open IOHandles after they are done with their potential raises.
221 """
--> 222 self.handles = get_handle(
223 src,
224 "r",
~anaconda3libsite-packagespandasiocommon.py in get_handle(path_or_buf, mode, encoding, compression, memory_map, is_text, errors, storage_options)
700 if ioargs.encoding and "b" not in ioargs.mode:
701 # Encoding
--> 702 handle = open(
703 handle,
704 ioargs.mode,
PermissionError: [Errno 13] Permission denied: 'C:\Users\jeppe\bot\data\.ipynb_checkpoints'
[1]: https://i.stack.imgur.com/6a3q0.png
有必要排除.ipynb_checkpoints文件,这可以通过这个简单的for循环来实现
for file in filenames:
if not file.startswith("."):
df = pd.read_csv(f'{raw_data_path}{file}')