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