所以我试图打开并操作存储在我的Django应用程序的MEDIA_ROOT
目录中的用户上传文件。
截至目前,我只能打开项目目录中的文件。我尝试访问Data_Files
目录中media
目录中的文件如下:
file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'Data_Files')
data_file = open(os.path.join(file_upload_dir, new_file), 'rb')
raw_data = data_file.read()
file_upload_dir = os.path.join(settings.MEDIA_ROOT, 'Data_Files')
data_file = default_storage.open(os.path.join(file_upload_dir, new_file), 'r')
raw_data = data_file.read()
我的第二次尝试利用了Django的FileStorage API
,但是除了项目目录中的文件之外,它们都不允许我访问文件。
给出的错误是:
file b'PVP3.CSV' does not exist
带有追溯:
Environment:
Request Method: POST
Request URL: http://127.0.0.1:8000/graphite/file_upload/
Django Version: 1.7.1
Python Version: 3.4.2
Installed Applications:
('django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',
'django.contrib.sessions',
'django.contrib.messages',
'django.contrib.staticfiles',
'graphite',
'debug_toolbar')
Installed Middleware:
('django.middleware.cache.UpdateCacheMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.auth.middleware.SessionAuthenticationMiddleware',
'debug_toolbar.middleware.DebugToolbarMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django.middleware.cache.FetchFromCacheMiddleware')
Traceback:
File "C:\Python34\lib\site-packages\django-1.7.1-py3.4.egg\django\core\handlers\base.py" in get_response
111. response = wrapped_callback(request, *callback_args, **callback_kwargs)
File "C:\Python34\lib\site-packages\django-1.7.1-py3.4.egg\django\contrib\auth\decorators.py" in _wrapped_view
22. return view_func(request, *args, **kwargs)
File "C:\Users\vut46744\Desktop\graphite_project\graphite\views.py" in upload_file
112. data_df = html_graph(request)
File "C:\Users\vut46744\Desktop\graphite_project\graphite\views.py" in html_graph
310. data_df = pd.read_csv(new_file, skiprows=header_row).drop(0, axis=0).astype(float)
File "C:\Python34\lib\site-packages\pandas\io\parsers.py" in parser_f
465. return _read(filepath_or_buffer, kwds)
File "C:\Python34\lib\site-packages\pandas\io\parsers.py" in _read
241. parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Python34\lib\site-packages\pandas\io\parsers.py" in __init__
557. self._make_engine(self.engine)
File "C:\Python34\lib\site-packages\pandas\io\parsers.py" in _make_engine
694. self._engine = CParserWrapper(self.f, **self.options)
File "C:\Python34\lib\site-packages\pandas\io\parsers.py" in __init__
1056. self._reader = _parser.TextReader(src, **kwds)
Exception Type: OSError at /graphite/file_upload/
Exception Value: File b'PVP3.CSV' does not exist
是否有不同的方式来访问这些文件,或者我的代码是否存在逻辑错误?