Forgot password?
 Create new account
View 127|Reply 2

去除 php 末尾 ?> 后的空行

[Copy link]

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

hbghlyj Posted at 2025-3-18 02:50:59 |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

701

Threads

110K

Posts

910K

Credits

Credits
94145
QQ

Show all posts

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

3148

Threads

8489

Posts

610K

Credits

Credits
66148
QQ

Show all posts

 Author| hbghlyj Posted at 2025-3-18 06:57:55
GitHub 不显示末尾空行。下载后可见

手机版Mobile version|Leisure Math Forum

2025-4-20 11:40 GMT+8

Powered by Discuz!

× Quick Reply To Top Return to the list