简体   繁体   English

使用Python删除文件夹中2个或3个文件中的每1个

[英]Delete every 1 of 2 or 3 files on a folder with Python

What I'm trying to do is to write a code that will delete a single one of 2 [or 3] files on a folder. 我想做的是编写一个代码,该代码将删除文件夹中2 [或3]个文件中的一个。 I have batch renamed that the file names are incrementing like 0.jpg, 1.jpg, 2.jpg... n.jpg and so on. 我已批量重命名了文件名,如0.jpg,1.jpg,2.jpg ... n.jpg等。 What I had in mind for the every single of two files scenario was to use something like "if %2 == 0" but couldn't figure out how actually to remove the files from the list object and my folder obviously. 对于两个文件中的每一个,我想到的是使用“ if%2 == 0”之类的方法,但无法弄清楚如何实际上从列表对象和文件夹中删除文件。

Below is the piece of NON-WORKING code. 下面是一段非工作代码。 I guess, it is not working as the file_name is a str. 我想这是行不通的,因为file_name是一个str。

import os

os.chdir('path_to_my_folder')

for f in os.listdir():
    file_name, file_ext = os.path.splitext(f)
    print(file_name)

    if file_name%2 == 0:
      os.remove();

Yes, that's your problem: you're trying to use an integer function on a string. 是的,这就是您的问题:您正在尝试对字符串使用整数函数。 SImply convert: 立即转换:

if int(file_name)%2 == 0:

... that should fix your current problem. ...应该可以解决您当前的问题。

Your filename is a string, like '0.jpg' , and you can't % 2 a string. 您的文件名是一个字符串,例如'0.jpg' ,并且您不能% 2一个字符串。 1 1个

What you want to do is pull the number out of the filename, like this: 您要执行的操作是从文件名中拉出数字,如下所示:

name, ext = os.path.splitext(filename)
number = int(name)

And now, you can use number % 2 . 现在,您可以使用number % 2

(Of course this only works if every file in the directory is named in the format N.jpg , where N is an integer; otherwise you'll get a ValueError .) (当然,这仅在目录中的每个文件都以N.jpg格式命名N.jpg ,其中N是整数;否则,将出现ValueError 。)


1. Actually, you can do that, it just doesn't do what you want. 1.实际上,您可以做到这一点,但是它并没有做您想要的。 For strings, % means printf-style formatting, so filename % 2 means “find the %d or similar format spec in filename and replace it with a string version of 2 . 对于字符串, %表示printf样式的格式,因此filename % 2表示“在filename查找%d或类似格式规范,并将其替换为字符串版本2

Thanks a lot for the answers! 非常感谢您的回答! I have amended the code and now it looks like this; 我已经修改了代码,现在看起来像这样;

import os

os.chdir('path_to_the_folder')

for f in os.listdir():
  name, ext = os.path.splitext(f)
  number = int(name)

if number % 2 == 0:
    os.remove()

It doesn't give an error but it also doesn't remove/delete the files from the folder. 它不会产生错误,但是也不会从文件夹中删除/删除文件。 What in the end I want to achieve is that every file name which is divisible by two will be removed so only 1.jpg, 3.jpg, 5.jpg and so on will remain. 最后,我要实现的是将删除每个可被两个整除的文件名,因此仅保留1.jpg,3.jpg,5.jpg等。

Thanks so much for your time. 非常感谢您的宝贵时间。

A non-Python method but sharing for future references; 非Python方法,但可以共享以备将来参考;

cd path_to_your_folder
mkdir odd; mv *[13579].png odd

also works os OSX. 也适用于OS OSX。 This reverses the file order but that can be re-corrected easily. 这样可以颠倒文件顺序,但是可以很容易地对其进行更正。 Still want to manage this within Python though! 不过还是要在Python中管理它!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM