Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
T
txt_tools
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
zhou
txt_tools
Commits
743a5ba6
Commit
743a5ba6
authored
Sep 02, 2019
by
Zhouxingyu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
123
parents
Pipeline
#35
failed with stages
Changes
2
Pipelines
1
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
72 additions
and
0 deletions
+72
-0
将当前文件夹的所有txt转化为utf-8格式.py
将当前文件夹的所有txt转化为utf-8格式.py
+56
-0
提取文件夹中所有txt放入另外一个文件夹中.py
提取文件夹中所有txt放入另外一个文件夹中.py
+16
-0
No files found.
将当前文件夹的所有txt转化为utf-8格式.py
0 → 100644
View file @
743a5ba6
import
os
import
codecs
import
chardet
def
list_folders_files
(
path
):
"""
返回 "文件夹" 和 "文件" 名字
:param path: "文件夹"和"文件"所在的路径
:return: (list_folders, list_files)
:list_folders: 文件夹
:list_files: 文件
"""
list_folders
=
[]
list_files
=
[]
for
file
in
os
.
listdir
(
path
):
file_path
=
os
.
path
.
join
(
path
,
file
)
if
os
.
path
.
isdir
(
file_path
):
list_folders
.
append
(
file
)
else
:
list_files
.
append
(
file
)
return
(
list_folders
,
list_files
)
def
convert
(
file
,
in_enc
=
"GBK"
,
out_enc
=
"UTF-8"
):
"""
该程序用于将目录下的文件从指定格式转换到指定格式,默认的是GBK转到utf-8
:param file: 文件路径
:param in_enc: 输入文件格式
:param out_enc: 输出文件格式
:return:
"""
try
:
in_enc
=
in_enc
.
upper
()
out_enc
=
out_enc
.
upper
()
except
AttributeError
:
return
0
try
:
print
(
"convert [ "
+
file
.
split
(
'
\\
'
)[
-
1
]
+
" ].....From "
+
in_enc
+
" --> "
+
out_enc
)
f
=
codecs
.
open
(
file
,
'r'
,
in_enc
,
"ignore"
)
new_content
=
f
.
read
()
codecs
.
open
(
file
,
'w'
,
out_enc
)
.
write
(
new_content
)
except
IOError
as
err
:
print
(
"I/O error: {0}"
.
format
(
err
))
# 将路径下面的所有文件,从原来的格式变为UTF-8的格式
if
__name__
==
"__main__"
:
path
=
r'D:\Users\86183\Desktop\pic083'
#只要满足形式,一般只需改变文件夹的路径即可
(
list_folders
,
list_files
)
=
list_folders_files
(
path
)
print
(
"Path: "
+
path
)
for
fileName
in
list_files
:
filePath
=
path
+
'
\\
'
+
fileName
with
open
(
filePath
,
"rb"
)
as
f
:
data
=
f
.
read
()
codeType
=
chardet
.
detect
(
data
)[
'encoding'
]
convert
(
filePath
,
codeType
,
'UTF-8'
)
提取文件夹中所有txt放入另外一个文件夹中.py
0 → 100644
View file @
743a5ba6
import
os
import
shutil
print
(
'输入格式:E:
\
myprojectnew
\
jupyter
\
整理文件夹
\
示例'
)
path
=
input
(
'请键入需要整理的文件夹地址:'
)
new_path
=
input
(
'请键入要复制到的文件夹地址:'
)
for
root
,
dirs
,
files
in
os
.
walk
(
path
):
for
i
in
range
(
len
(
files
)):
#print(files[i])
if
(
files
[
i
][
-
3
:]
==
'txt'
)
:
file_path
=
root
+
'/'
+
files
[
i
]
new_file_path
=
new_path
+
'/'
+
files
[
i
]
shutil
.
copy
(
file_path
,
new_file_path
)
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment