大写python目录中多个文件的名称
我正在研究一个小型项目,该项目要求我使用python在某个目录中使用“ ex:输入:brandy.jpg,output:brandy.jpg”中的所有文件名称。 问题是我以前从未在多个文件上完成过,我所做的是以下内容:
universe = os.listdir('parallel_universe/')
universe = [os.path.splitext(x)[0].upper() for x in universe]
但是我所做的只是在列表中的名称仅但是目录本身中的文件,输出就像以下内容:
['ADAM SANDLER','ANGELINA JULIE','ARIANA GRANDE','BEN AFFLECK','BEN STILLER','BILL GATES', 'BRAD PITT','BRITNEY SPEARS','BRUCE LEE','CAMERON DIAZ','DWAYNE JOHNSON','ELON MUSK','ELTON JOHN','JACK BLACK','JACKIE CHAN','JAMIE FOXX','JASON SEGEL', 'JASON STATHAM']
我在这里想念什么?而且,由于我在Python上没有太多经验,因此我希望您的答案包括每个步骤的解释,并提前感谢。
I'm working on a small project that requires that I use Python to uppercase all the names of files in a certain directory "ex: input: Brandy.jpg , output: BRANDY.jpg".
The thing is I've never done on multiple files before, what I've done was the following:
universe = os.listdir('parallel_universe/')
universe = [os.path.splitext(x)[0].upper() for x in universe]
But what I've done capitalized the names in the list only but not the files in the directory itself, the output was like the following:
['ADAM SANDLER','ANGELINA JULIE','ARIANA GRANDE','BEN AFFLECK','BEN STILLER','BILL GATES', 'BRAD PITT','BRITNEY SPEARS','BRUCE LEE','CAMERON DIAZ','DWAYNE JOHNSON','ELON MUSK','ELTON JOHN','JACK BLACK','JACKIE CHAN','JAMIE FOXX','JASON SEGEL', 'JASON STATHAM']
What am I missing here? And since I don't have much experience in Python, I'd love if your answers include explanations for each step, and thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
现在,您将字符串转换为大写,但仅此而已。没有实际的重命名。为了重命名,您需要使用
os.Rename
如果要使用
OS.RENAME
将代码包装,则应解决您的问题,例如:我已删除分配
Universe =
因为此行不再返回列表,而您将在none
对象上获得一堆。os.rename
的文档: https:/ /docs.python.org/3/library/os.html#os.renameRight now, you are converting the strings to uppercase, but that's it. There is no actual renaming being done. In order to rename, you need to use
os.rename
If you were to wrap your code with
os.rename
, it should solve your problem, like so:I have removed the assignment
universe=
because this line no longer returns a list and you will instead get a bunch onNone
objects.Docs for
os.rename
: https://docs.python.org/3/library/os.html#os.rename