[英]How to open a file with Python on Windows with FileShare permissions?
[英]Opening file from a Fileshare-system with python
我试图从我们公司的文件共享系统中打开一个文件。 这是脚本的代码:
import sys
import xlrd
from tkinter import *
import pandas as pd
import time
from os import *
#hvl_file_path = filedialog.askopenfilename()
save_path = filedialog.asksaveasfilename(initialdir="C:/", defaultextension=".xlsx")
t1 = time.clock()
hvl = pd.read_csv('\\-----Berichte_SQL\HVL.csv', sep='|', encoding='latin1', low_memory=False)
hvl.to_excel(save_path, index=False)
t2 = time.clock()
t_ges = t2 - t1
print(t_ges)
可以看到file_path是:\\-----\\Berichte_SQL\\HVL.csv
当我启动脚本时,出现以下错误:
Traceback (most recent call last):
File "C:/Users/-----/Desktop/PROJEKTE/[INPROGRESS] AUSWERTUNG COIN + HVL/hvl_convert.py", line 13, in <module>
hvl = pd.read_csv('------\Berichte_SQL\HVL.csv', sep='|', encoding='latin1', low_memory=False)
File "C:\Users\------\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\pandas\io\parsers.py", line 474, in parser_f
return _read(filepath_or_buffer, kwds)
File "C:\Users\------\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\pandas\io\parsers.py", line 250, in _read
parser = TextFileReader(filepath_or_buffer, **kwds)
File "C:\Users\------\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\pandas\io\parsers.py", line 566, in __init__
self._make_engine(self.engine)
File "C:\Users\------------\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\pandas\io\parsers.py", line 705, in _make_engine
self._engine = CParserWrapper(self.f, **self.options)
File "C:\Users\----------\AppData\Local\Downloaded Apps\Winpython\python-3.4.3\lib\site-packages\pandas\io\parsers.py", line 1072, in __init__
self._reader = _parser.TextReader(src, **kwds)
File "pandas\parser.pyx", line 350, in pandas.parser.TextReader.__cinit__ (pandas\parser.c:3187)
File "pandas\parser.pyx", line 594, in pandas.parser.TextReader._setup_parser_source (pandas\parser.c:5930)
OSError: File b'----------\\Berichte_SQL\\HVL.csv' does not exist
所以我的问题是路径是否存在问题或缺少权限。 你有什么想法?
谢谢!
编辑:
现在我尝试打开文件:
f = open('\\---------\Berichte_SQL\HVL.csv','w')
尝试使用 open 打开文件后,出现此错误:
Traceback (most recent call last):
File "C:/Users/--------ROJEKTE/[INPROGRESS] AUSWERTUNG COIN + HVL/hvl_convert.py", line 13, in <module>
f = open('\\---------------\Berichte_SQL\HVL.csv','w')
TypeError: an integer is required (got type str)
通常在处理 UNC 字符串或 Windows 字符串时,最好使用r
(原始)前缀声明常量字符串:
pd.read_csv(r'\\Q4DEE1SYVFS.ffm ...')
不这样做会导致一些字符被解释:
print("foo\test")
产量:
foo est (tabulation has been inserted)
许多小写字符(`\\t,\\n,\\v,\\b,\\x ...)也是如此。
双反斜杠表示“转义反斜杠”并转换为单个反斜杠。
print('\\ddd')
产量:
\ddd
因此您的路径不正确。
但这里还有更多。 你不应该得到expected int found str
错误。 所以我在您的评论之一中发现了问题:有一些不可见的字符会引起麻烦。 我将您评论中的路径粘贴到 pyscripter 中,并将其分配给一个变量,然后执行以下操作:
>>> z=r"\\Q4DEE1SYVFS.ffm.t-systems.com\pasm$\Berichte_SQL\HVL.csv"
>>> z
'\\\\Q4DEE1SYVFS.ffm.t-systems.com\\pasm$\\Berichte_SQL\\HVL\xe2\x80\x8c\xe2\x80\x8b.csv'
只需重写字符串的最后一部分,它就会起作用。
PS:记事本++无法看到奇怪的字符。 我听说 SciTe 也倾向于让这些通过。 Pyscripter 看到了它们。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.