The important thing in life is to have a great aim , and the determination

          常用鏈接

          統計

          IT技術鏈接

          保險相關

          友情鏈接

          基金知識

          生活相關

          最新評論

          python操作文件存在的問題解決辦法

          1、在學習從文件讀取數據中,寫了個示例代碼,讀取不在同一個目錄的file.txt,運行后報這個Python OSError: [Errno 22] Invalid argument:錯誤: (1)、首先,在F盤的python_stu中新增了一個file.txt,同時在F盤的python_stu文件目錄底下新增一個file文件夾,里面有個file_reader.py來讀取python_stu文件目錄底下的file.txt,代碼分別如下: file.txt: 測試 測試2 測試3 file_reader.py: with open('F:\python_stu\file.txt') as file_obj: contents = file_obj.read(); print(contents.rstrip()); (2)、運行后報錯: (3)、出現這種錯誤的原因是由于讀取不到這個文件,看Traceback報的錯誤,最后一行,很明顯讀取不到file.txt,前面的F:\\python_stu沒錯,后面的名稱怎么變了,還是x0cile.txt。 (4)、解決辦法,可修改上述第一行代碼為: with open('F:\python_stu/file.txt') as file_obj: 或者: with open('F:/python_stu/file.txt') as file_obj: 或者: with open('F://python_stu//file.txt') as file_obj: 又或者: with open('F:\\python_stu\\file.txt') as file_obj: 還有一些我就不附上了,上面第一種方式不統一,最好不要用,用統一的方式,而且有時候還有注意一些轉義字符,比如 \t,\n也會導致報錯。

          posted on 2019-05-14 23:04 鴻雁 閱讀(161) 評論(0)  編輯  收藏 所屬分類: IT技術相關

          主站蜘蛛池模板: 万荣县| 石林| 伊川县| 灵山县| 响水县| 安福县| 蒙山县| 昔阳县| 阿拉善右旗| 南乐县| 八宿县| 泽库县| 邓州市| 于都县| 铜川市| 日土县| 松桃| 陇西县| 天全县| 来凤县| 湘潭市| 含山县| 馆陶县| 曲水县| 滨州市| 准格尔旗| 玛沁县| 佳木斯市| 高陵县| 兴和县| 沅陵县| 体育| 通州市| 汝阳县| 横山县| 平泉县| 乌拉特中旗| 望奎县| 桑植县| 高唐县| 金川县|