问题描述
我有一个小问题.
我有以下功能:
def getCommands():
for file in os.listdir(com_dir):
if file.endswith(com_ext):
z = string.strip(file, '.gcom')
print z
并且在目录(由 com_dir
定义)中有三个文件.
and in the directory ( Defined by com_dir
) there are three files.
a.gcom
b.gcom
c.gcom
运行时getCommands()
输出如下:
明智的变量.
Files a and b are shown ,however, c is not shown, All files are in the directory and all are using the same file extension: .gcom
which is also com_ext
variable wise.
有人对为什么文件 c 没有显示有任何提示吗?
Does anyone have any hints as to why file c is not being shown?
旁注:输出中似乎有一个空格,其中 c
应该是但是我不确定这是否与手头的问题有任何关系,而不仅仅是一个偶然的空间放置在脚本的其他位置.
Side note: There seems to be a blank space in the output where c
should be however Im not sure if this has any part in the issue at hand and isn't just simply a accidental space placed elsewhere in the script.
推荐答案
strip
从字符串的两端删除所有给定的字符,无论它们出现的顺序如何.如果您的字符串是 c.gcom
,然后 strip('.gcom')
删除所有字符 .
, g
, c
, o
和 m
从你的字符串的末尾,不留下任何东西.它不会停止剥离,直到遇到一个不是 .
、g
、c
、o
或m
(或删除所有内容).
strip
removes all the given characters from both ends of your string, whatever order they occur in. If your string is c.gcom
, then strip('.gcom')
removes all the characters .
, g
, c
, o
and m
from the ends of your string, leaving nothing left. It doesn't stop stripping until it hits a character that is not .
, g
, c
, o
or m
(or removes everything).
如果您有一个以 .gcom
结尾的字符串,而您只想删除该结尾,您可以使用:
If you have a string ending in .gcom
, and you just want to remove that ending, you can use:
z = file[:-5]
或者,使用您的 com_ext
变量
or, using your com_ext
variable
com_ext = '.gcom'
...
if file.endswith(com_ext):
z = file[:-len(com_ext)]
这篇关于Python 脚本没有列出目录中的文件?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!