withopen("example.txt", 'w') as file: file.write("This is a test file.")
給定文字檔,每三行就印出來
1 2 3 4 5 6 7 8
defprint_3rd_line(file_path) ->str: result = [] withopen(file_path, 'r') as file: for i, line inenumerate(file): if (i+1)%3 == 0: print(line) result.append(line) return'\n'.join(result)
給定文字檔,印出該檔案有幾行
1 2 3 4 5 6
defprint_line(file_path) ->str: result = 0 withopen(file_path, 'r') as file: for i, line inenumerate(file): result = i return result
給定文字檔,印出該檔案有幾個字彙(word)
1 2 3 4 5
defprint_word(file_path) ->str: withopen(file_path, 'r') as file: text =file.read() words = text.split() returnlen(words)
將一段文字寫入到已開啟的檔案最末端
1 2 3
text = "A brown fox jumped over the lazy dog." withopen('example.txt', 'a') as file: file.write('\n' +text + '\n')
withopen("example.json", "w") as file: json.dump(data, file)
pprint(data)
使用 Pathlib
印出當前目錄下的檔案內容
1 2 3 4 5 6
import pathlib import os
path = pathlib.Path(os.getcwd()+'/example.txt') text = path.read_text() print(text)
寫入字串到檔案
1 2 3 4 5 6 7 8
import pathlib import os
text = "A quick brown fox jumped over the dog." path = pathlib.Path(os.getcwd() + '/example.txt') path.write_text(text) story = path.read_text() print(story)
OS operations
python 中的 os 模組提供了許多low level 的作業系統 system call,並且在多種作業系統中提供一個統一的調用介面。
match = re.search(r'(\w+)@(\w+)\.(\w+)', mail_list) print(match.group(0))
## Output # kevin@amazon.com
為了更加方便存取返回的匹配物件,可以透過 ?P<NAME> 來去幫group命名
1 2 3 4 5 6 7
import re
match = re.search(r'(?P<name>\w+)@(?P<SLD>\w+)\.(?P<TLD>\w+)', mail_list) print(match.group("name"))
## Output # kevin
如何在一個檔案中找到所有 IP Address?
對於這樣的 input
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In the ever-evolving digital landscape, tracking online data often requires observing user interactions from various regions. For instance, a significant number of logins were recorded from IP addresses like 24.31.173.2 and 82.33.11.118, which reflect diverse user origins. Security teams frequently monitor these access points, noting unusual activity from IPs such as 45.56.148.51 and 99.116.96.74.
On an average day, they might encounter new sessions from endpoints like 24.15.139.93 or 67.166.35.191, each needing real-time analysis to ensure data protection. In one case, an IP address from the Asia-Pacific region, 180.215.121.104, demonstrated a high frequency of requests, which raised some flags in the system.
Europe is also represented in traffic logs, with IPs such as 64.130.155.78 and 186.176.159.218 originating from locations that maintain stringent data compliance regulations. The North American region had frequent logins from addresses like 71.239.86.182 and 69.43.251.147, where several were validated for secure access.
Moreover, a pattern was observed with IPs like 100.12.201.224 and 85.164.201.224 accessing confidential systems during peak hours. Meanwhile, analysts in cybersecurity teams often encounter unusual log requests from regions like 120.156.7.200, necessitating further investigation.
The logs also revealed consistent access from IPs like 24.61.142.106, which had been previously flagged for unauthorized attempts. Similarly, 99.58.40.206 and 70.15.237.26 showed a surge in activity, prompting immediate protocol checks. Another example is the IP 108.205.48.11, detected during an audit of high-value transactions.
Many more instances, such as connections from 72.168.129.149 and 189.224.44.46, add complexity to monitoring systems. Security teams must evaluate each source, even those with benign records like 68.60.26.228 or frequent accesses from 24.236.160.189.
Moreover, the system recorded remote sessions from addresses like 100.36.162.76 and 47.16.121.249, both requiring location verification steps. High-risk IPs, such as 184.4.175.60 and 62.19.65.98, were flagged during the latest vulnerability scan, which further highlighted global risks.
Unusual activity from Latin America was seen with addresses like 186.35.108.144, while other regions showed minimal access, as with 189.24.118.68. IPs from North America, like 71.75.81.146 and 72.89.236.44, represent legitimate access but require continuous monitoring. For example, sessions from 174.125.174.95 often triggered notifications, especially those attempting to access privileged systems.
A deeper look at international traffic unveiled access from IP 190.244.119.142, a significant address due to its activity level. Occasional spikes were noted from Europe, with entries from IP 84.26.122.138, which was scrutinized during regular audits.
Lastly, domestic IPs like 65.112.10.60 and overseas ones such as 108.71.92.240 reveal the complexity of maintaining cybersecurity in a global context. One user from an IP at 1.43.41.29 exhibited typical behavior but underscored the need for constant vigilance.
1 2 3 4 5 6 7 8 9 10
import re
deffind_IP(file_Path): withopen(file_Path, "r") as file: text = file.read() match = re.findall(r'(\d+)\.(\d+).(\d+)\.(\d+)', text) ip_list = ['.'.join(ip) for ip inmatch] return ip_list