python 打开文件,读取文件内容
生活随笔
收集整理的這篇文章主要介紹了
python 打开文件,读取文件内容
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
#include <iostream>
#include <fstream>
using namespace std;int main()
{int x,sum=0;ifstream srcFile("result.txt", ios::in); //以文本模式打開in.txt備讀if (!srcFile) { //打開失敗cout << "error opening source file." << endl;return 0;}ofstream destFile("out.txt", ios::out); //以文本模式打開out.txt備寫if (!destFile) {srcFile.close(); //程序結束前不能忘記關閉以前打開過的文件cout << "error opening destination file." << endl;return 0;}//可以像用cin那樣用ifstream對象while (srcFile >> x) {sum += x;//可以像 cout 那樣使用 ofstream 對象destFile << x << " ";}cout << "sum:" << sum << endl;destFile.close();srcFile.close();return 0;
}
文件里內容就是上述的數字,并且進行了求和運算。通過上述腳本可以簡單的實現,讀取文件內容的功能。
與50位技術專家面對面20年技術見證,附贈技術全景圖總結
以上是生活随笔為你收集整理的python 打开文件,读取文件内容的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Bulk API实现批量操作
- 下一篇: websocket python爬虫_p