Forgot password?
 Register account
View 266|Reply 2

去除 php 末尾 ?> 后的空行

[Copy link]

3152

Threads

7905

Posts

610K

Credits

Credits
64068
QQ

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

682

Threads

110K

Posts

910K

Credits

Credits
90973
QQ

Show all posts

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

3152

Threads

7905

Posts

610K

Credits

Credits
64068
QQ

Show all posts

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

Mobile version|Discuz Math Forum

2025-6-4 17:13 GMT+8

Powered by Discuz!

× Quick Reply To Top Edit