iT邦幫忙

1

(已解決)python寫入的txt檔案會存放在哪?

4yc 2023-03-04 17:55:081188 瀏覽
  • 分享至 

  • xImage
import requests
from bs4 import BeautifulSoup 

url='https://www.ptt.cc/bbs/Gossiping/index.html'
web=requests.get('https://www.ptt.cc/bbs/Gossiping/index.html',cookies={'over18':'1'})
web.encoding='utf-8'
soup=BeautifulSoup(web.text,'html.parser') #網頁解析器
![https://ithelp.ithome.com.tw/upload/images/20230304/20157661VfweQMc6Z0.jpg](https://ithelp.ithome.com.tw/upload/images/20230304/20157661VfweQMc6Z0.jpg)
titles=soup.find_all('div',class_='title')
output=""
for i in titles:
    output=output+i.find('a').get_text()+'\n'+i.find('a')['href']+'\n'
print(output)

f=open('pptdata.txt','w+')
f.write(output)
f.close()

問題:
已將爬蟲完畢的檔案寫入文字檔中,請問我應該去哪裡找這個文字檔?
https://ithelp.ithome.com.tw/upload/images/20230304/20157661sJjAe8y42I.jpg

看更多先前的討論...收起先前的討論...
froce iT邦大師 1 級 ‧ 2023-03-04 18:30:54 檢舉
c:\Users\amand

去搞懂工作目錄是啥吧
ccutmis iT邦高手 2 級 ‧ 2023-03-04 19:44:24 檢舉
補充:
還有建議重裝 Python ,在安裝時記得勾選"Add Python to PATH",或是不重裝的話就把Python安裝路徑加到環境變數"PATH"裡面,執行的時候只需要打"Python ptt.py" 這樣才是比較正常的作法。
bill0704 iT邦新手 5 級 ‧ 2023-03-05 18:14:18 檢舉
import os
os.getcwd()
到這裏找一下
player iT邦大師 1 級 ‧ 2023-03-06 14:08:11 檢舉
你開檔案時
不能把資料夾路徑與檔名
一起當參數一起傳進去嗎?
搞到還得找工作資料夾在哪裡?
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中
3
貓虎皮
iT邦新手 3 級 ‧ 2023-03-04 23:48:20
最佳解答

python的檔案讀寫操作會在「工作目錄(working directory)」進行,
所以從你當前的執行位置來看的話應該會存放在「C:\Users\amand\」底下,
也就是檔案位置會在「C:\Users\amand\pptdata.txt」。

你可以透過在終端機執行「cd」指令(change directory)來切換當前的工作目錄,
如果你要讓輸出之文字檔存放於目前python檔的父層資料夾(....../project/ppt)下的話可以這樣執行:

cd d:/Python-training/project/ppt/
C:/Users/amand/AppData/Local/Programs/Python/Python310/python.exe ppt.py

或者也可以透過pythonos模組來達成,在python檔最前面加上:

import os
os.chdir('d:/Python-training/project/ppt/')

希望有幫助到您=^w^=

2
japhenchen
iT邦超人 1 級 ‧ 2023-03-06 07:32:33

在你的windows裡安裝個everything這套工具,就不用到處找檔案了

0
心累
iT邦新手 5 級 ‧ 2023-03-08 16:54:40

1.不是"問提",是"問題"
2.在工作目錄
3.比起讓你知道工作目錄,不如下次自己指定位置

f=open('c:/Users/amand/pptdata.txt','w+')

我要發表回答

立即登入回答