问题描述
我正在编写一个python脚本,用户创建目录
输入。
有时用户输入的字符不是
文件或目录名。
以下是我认为有效字符的字符......
有效=
'':./,^ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL MNOPQRSTUVWXYZ''
如果我有一个名为fname的字符串我想通过
$ b中的每个字符$ b文件名,如果它不是有效字符,那么我想用空格替换
。
这就是我所拥有的:
def fixfilename(fname):
有效=
'':。\,^ 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGH IJKLMNOPQRSTUVWXYZ''
for i in range(len(fname)):
if valid.find(fname [i])< 0:
fname [i] =''''
返回fname
有人想到更简单的解决方案吗? />
''123123.txt''
-
Jerry
如果你想剥离''em:
Hi I''m writing a python script that creates directories from user
input.
Sometimes the user inputs characters that aren''t valid characters for a
file or directory name.
Here are the characters that I consider to be valid characters...
valid =
'':./,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKL MNOPQRSTUVWXYZ ''
if I have a string called fname I want to go through each character in
the filename and if it is not a valid character, then I want to replace
it with a space.
This is what I have:
def fixfilename(fname):
valid =
'':.\,^0123456789abcdefghijklmnopqrstuvwxyzABCDEFGH IJKLMNOPQRSTUVWXYZ ''
for i in range(len(fname)):
if valid.find(fname[i]) < 0:
fname[i] = '' ''
return fname
Anyone think of a simpler solution?
''123123.txt''
--
Jerry
If you want to strip ''em:
这篇关于制作有效的文件名......的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!