os.getcwd() 导致 FileNotFoundError

os.getcwd() leading to FileNotFoundError

我正在尝试将数据从 csv 导入数据库 following this repository

我 运行 我的 windows 桌面上的功能,并得到这个错误:

['Customer Contracts$.csv', 'Customer Demo.csv', 'Customer Engagements.csv']
C:\Users\User\Code\DatabaseImporter/datasets/
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
~\AppData\Local\Temp/ipykernel_17908/1233121651.py in <module>
     16 print(csv_files)
     17 configure_dataset_directory(csv_files, dataset_dir)
---> 18 df = create_df(dataset_dir, csv_files)
     19 
     20 for k in csv_files:

~\Code\DatabaseImporter\csv_import_functions.py in create_df(dataset_dir, csv_files)
     40     for file in csv_files:
     41         try:
---> 42             df[file] = pd.read_csv(data_path+file)
     43         except UnicodeDecodeError:
     44             df[file] = pd.read_csv(data_path+file, encoding="ISO-8859-1") #if utf-8 encoding error

~\anaconda3\lib\site-packages\pandas\util\_decorators.py in wrapper(*args, **kwargs)
    309                     stacklevel=stacklevel,
    310                 )
--> 311             return func(*args, **kwargs)
    312 
    313         return wrapper

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.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 

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.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:

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.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):

~\anaconda3\lib\site-packages\pandas\io\parsers\readers.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):

~\anaconda3\lib\site-packages\pandas\io\parsers\c_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 

~\anaconda3\lib\site-packages\pandas\io\parsers\base_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",

~\anaconda3\lib\site-packages\pandas\io\common.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,

FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\User\Code\DatabaseImporter/datasets/Customer Contracts$.csv'

我在他使用的视频中注意到 Mac。我认为这与错误有关。 我尝试使用 .replace 清理字符串,但没有得到任何不同的结果。

我创建了一些测试代码,发现了一些有趣的东西。

import os
print(os.getcwd())
os.getcwd()

C:\Users\User\Code
'C:\Users\User\Code'

我认为这也只是问题的一半,因为最后的错误中有不同的斜线。

使用os.path.join而不是特定的串联

使用 shutil 移动(而不是 MV)

使用os.mkdir创建目录

在 mkdir 的错误处理中指定 FileExistsError