Forgot password
 Register account
View 247|Reply 2

去除 php 末尾 ?> 后的空行

[Copy link]

3175

Threads

7936

Posts

48

Reputation

Show all posts

hbghlyj posted 2025-3-18 02:50 |Read mode
若换行符为\r\n
  1. import os
  2. for root, dirs, files in os.walk(os.getcwd()):
  3.     for file in files:
  4.         if file.endswith('.php'):
  5.             file_path = os.path.join(root, file)
  6.             with open(file_path, 'rb+') as file:
  7.                 file.seek(-4, os.SEEK_END)  # Go to the 4th last byte of the file
  8.                 last_bytes = file.read(4)
  9.                 if last_bytes == b'?>\r\n':
  10.                     print(file_path)
  11.                     file.seek(-2, os.SEEK_END)  # Go to the last 2 bytes (the newline)
  12.                     file.truncate()
Copy the Code
若换行符为\n,需将上面的 -4 改为 -3,将4改为3,将?>\r\n改为?>\n,将-2改为-1

678

Threads

110K

Posts

213

Reputation

Show all posts

kuing posted 2025-3-18 04:49
新合并的所有文件都多了空行吗?
🤔

3175

Threads

7936

Posts

48

Reputation

Show all posts

original poster hbghlyj posted 2025-3-18 06:57
GitHub 不显示末尾空行。下载后可见

Quick Reply

Advanced Mode
B Color Image Link Quote Code Smilies
You have to log in before you can reply Login | Register account

$\LaTeX$ formula tutorial

Mobile version

2025-7-1 17:57 GMT+8

Powered by Discuz!

Processed in 0.012719 seconds, 22 queries