當前位置:首頁 » 編程軟體 » txt去空格腳本

txt去空格腳本

發布時間: 2024-10-24 22:11:57

❶ 求個批量刪除文件內容空格的腳本

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

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:563
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:852
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:552
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:731
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:654
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:971
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:226
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:84
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:774
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:680