# My Python

### 顯示中文

```
# -*- coding: utf-8 -*-
```

參考資料：\
<https://blog.gtwang.org/programming/python-chinese-comments-utf8-encoding/>

```python
#讓這支程式在作為module被其他人當作模組呼叫與直接執行有所區別
#直接執行    -> name的名稱就是main
#模組呼叫    -> name則為test


#test.py
if __name__ == __main__:
    print(__name__)
    main()
```

[參考來源](http://technology-sea.blogspot.tw/2012/03/python-name-import.html)

#### **Input**

raw\_input() --> 輸入內容會被視為字串型態

input() --> 輸入內容會被視為變數、公式等

[參考來源](https://www.foolegg.com/how-to-get-the-users-input-in-python-with-input-or-raw_input-functions/)

#### lambda

方便的小function，可用於簡單的運算或判斷

```python
#python3

lambda_test=lambda a,b : a+b  #輸入參數:a,b ， 執行的任務為a+b
print(lambda_test(8,7)) #result=15
```

#### map

操作所有元素

```python
#python3
#map運算結果可以再另外指定型態

#統一把陣列中的數字從str轉換為int
print(list(map(int,['1','2']))) #result=[1,2]

#統一把陣列中的數字從int轉換為str
print(list(map(str,[3,4]))) #result=['3','4']

#與lambda結合
func = lambda x: x+2 #將輸入值(x)加2後輸出
print(list(map(func, [1,2,3]))) #result=[3,4,5]
```

#### filter

根據條件過濾元素

```
#python3
```

[參考來源](http://fcamel-fc.blogspot.tw/2011/08/python-1.html)

## 檔案操作

#### 刪除

```python
import os
os.remove("C:\\Folder\\alarm.txt")  # 刪除檔案

import shutil
if os.path.exists(dump_path_date):
    shutil.rmtree(dump_path_date)  # 刪除資料夾和檔案
```

<http://wiki.alarmchang.com/index.php?title=Python_刪除目錄和下面的所有檔案_使用_shutil.rmtree>

參考連結:\
<http://www.victorgau.com/?p=5586>

#### 檔案名稱與路徑

```python
# -*- coding: utf-8 -*-

import sys
import os

# 檔案路徑
print(__file__)  # D:/xxx/xxx.py
print(sys.argv[0])  # D:/xxx/xxx.py

# 檔案名稱
print(os.path.basename(__file__))  # xxx.py
print(os.path.basename(sys.argv[0]))  # xxx.py

os.path.normcase(path) # 調整為小寫
os.path.normpath(path) # 在windows中可以調整斜線

# 在windows中執行
a = 'D:/a\\b\\c'
print(os.path.normcase(a))  # 調整斜線和小寫 => d:\a\b\c
print(os.path.normpath(a))  # 調整斜線 => D:\a\b\c


# 在linux中執行
a = 'D:/a\\b\\c'
print(os.path.normcase(a))  # D:/a\b\c
print(os.path.normpath(a))  # D:/a\b\c
```

參考連結:\
<https://blog.csdn.net/xiemanR/article/details/72728142>

#### 檔案複製

```python
import shutil
# 複製檔案
shutil.copyfile('./123.txt', './456.txt')

# 直接複製整個資料夾，不用先創立料夾2
shutil.copytree('/1','/2')
```

<https://blog.csdn.net/qq_37634812/article/details/79206025>

<https://read01.com/zh-tw/DmjLP5.html#.Wtg_b4huaUk>

<https://itw01.com/VUUFESS.html>

#### 讀取與寫入

<https://blog.csdn.net/ztf312/article/details/47259805>

#### 檔案大小

<https://codeday.me/bug/20170321/6057.html>

<https://blog.csdn.net/u013419465/article/details/40583031>

#### Exception

輸出詳細的錯誤訊息

```python
import traceback

try:
        1/0
except Exception, e:
        exstr = traceback.format_exc()
```

### 路徑

#### windows vs linux

<https://ephrain.net/python-使用-os-path-處理路徑問題/>

#### 獲取作業系統

<https://imsardine.wordpress.com/2012/06/12/python-programming-environment-os/>

#### print

sep = 指定分隔符號

end = 指定結束符號

flush = True (顯示緩衝區內容)

<https://tw.saowen.com/a/2c71b8fcf2846c5f460c64db0065b1e3926276d2bc17ddcd140d2e9ae040a006>

#### 查詢套件

**List all installed packages - 列出所有已安裝的套件**

`$ pip freeze`

**Check specify package version - 查看某套件版本**

`$ pip freeze | grep Django`

<https://whhnote.blogspot.com/2013/05/python-list-pip-installed-packages-pip.html>

#### 反編譯

簡介

<https://www.itread01.com/content/1546897339.html>

[https://aji.tw/%E5%A6%82%E4%BD%95%E4%BF%9D%E8%AD%B7python%E5%8E%9F%E5%A7%8B%E7%A2%BC/](https://aji.tw/如何保護python原始碼/)

<https://www.zhihu.com/question/20069661>

<https://kknews.cc/zh-tw/code/2xojaxr.html>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://stb11816.gitbook.io/python_note/web.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
