簡體   English   中英

使用python從文件共享系統打開文件

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM