---
# System prepended metadata

title: 第十一堂課-開啟檔案
tags: [Python]

---

###### tags: `Python`

# 第十一堂課-開啟檔案

```python=
file = open("data.txt", "r")            #r:read(讀取)

for n in file:
    print(n, end = "")

file.close                              
```
:::spoiler 執行結果
![](https://i.imgur.com/9O3TR5U.png)
:::
```python=
file = open("data.txt", "r")  #r:read

for n in file:
    n2 = n.strip()                      #strip()去除換行符號\n
    print(n2)

file.close
```
:::spoiler 執行結果
![](https://i.imgur.com/K0i9kTb.png)
:::
```python=
data = "abc:def:xyz"

list = data.split(":")                  #split:分割
 
print(list)
print(list[0])
```
:::spoiler 執行結果
![](https://i.imgur.com/Qg5c8eu.png)
:::
```python=
file = open("member.txt", "r")

for n in file:
    list = n.strip().split(",")
    print(list)

file.close()
```
:::spoiler 執行結果
![](https://i.imgur.com/MzAxEVL.png)
:::
```python=
file = open("member.txt", "r")
name = input("帳號:")
pwd = input("密碼:")
chk = 0

for n in file:
    list = n.strip().split(",")

    if name == list[0] and pwd == list[1]:
        chk =1

if chk == 1:
    print("正確")
else:
    print("登入失敗")

file.close()
```
:::spoiler 執行結果
![](https://i.imgur.com/oy5Cqct.png)
:::
```python=
file = open("member2.csv", "r")
name = input("帳號:")
pwd = input("密碼:")
chk = 0

for n in file:
    list = n.strip().split(",")

    if name == list[0] and pwd == list[1]:
        chk =1

if chk == 1:
    print("正確")
else:
    print("登入失敗")

file.close()
```
:::spoiler 執行結果
![](https://i.imgur.com/rvhLBq4.png)
:::