txt去空格腳本
❶ 求個批量刪除文件內容空格的腳本
import os
def remove_first_line_and_spaces(file_path):
with open(file_path, 'r') as file:
# 讀取文件的第二行開始的所改含旦有內容
content = file.readlines()[1:]
# 刪除所有行的前導空格和尾隨空格
content = [line.strip() for line in content]
# 將新內容寫迴文件
with open(file_path, 'w') as file:
file.write('\n'.join(content))
def process_directory(directory_path):
for filename in os.listdir(directory_path):
# 構建完整的文件核擾路徑
file_path = os.path.join(directory_path, filename)
if os.path.isfile(file_path):
remove_first_line_and_spaces(file_path)
if __name__ == '__main__':
directory_path = 'path/to/directory'
process_directory(directory_path)
注意:請把 'path/to/directory' 替換為你要處理的目錄的實際路徑老哪。
❷ 如何刪除一個文件夾中所有txt文件中的空格
@echo off
setlocal EnableDelayedExpansion
for /f "delims=" %%a in ('dir /a-d/s/b *.txt') do (
set file=%%~na
set file=!file: =!
ren "%%~fna" "!file!%%~xa"
)
pause
以上代碼是去除文件名中的空格,文件名不變,請採納,謝謝!
❸ 寫bat批處理文件時如何刪除一個文本文件的所有空格謝了
@echooff&
(for/f"delims="%%ain(test.txt)do(
set"line=%%a"
set"line=!line:=!"
echo!line!
))>new.txt
原始文本文件名稱為test.txt,刪除空格後生成的新文本文件為new.txt
❹ 用按鍵精靈刪除txt中的空行和空格,代碼怎麼寫
您好,暫時沒有,個人認為是寫入數據時方法不對,正確的方法應該是
Dim 內容 = Array("1","2","2","2","2","2","2","2","2","2","2","2","2","2","2")
換行寫入txt文件("txtx","sss",內容)
Function 換行寫入txt文件(文件夾名,文件名,內容)
Import "cgc.lua"
Dim 文件路徑= GetSdcardDir()&"/"&文件夾名&"/"
Dim txt文件 = 文件路徑&文件名&".txt"
If Dir.Exist(文件路徑) = 0 Then
Call Dir.Create(文件路徑)// 創建文件夾
End If
If Dir.Exist(txt文件) = 0 Then
Call cgc.CreateFile(txt文件)// 創建文件
End If
For i = 1 To UBound(內容)+1
If i = UBOUND(內容) + 1 Then
Call File.Append(txt文件, 內容(i)) //Chr(13)
Else
Call File.Append(txt文件, 內容(i)&" ") //Chr(13)
End If
Next
End Function