Python 如何获取文件夹中文件的序列号?
我有Windows PC。我的脚本应该识别在文件夹的命令行中传递的文件的顺序号,即Python 如何获取文件夹中文件的序列号?,python,Python,我有Windows PC。我的脚本应该识别在文件夹的命令行中传递的文件的顺序号,即 myscript.py \\network-drive\files\Long-long.file.name.with.numbers.txt 文件夹内容如下所示: \\network-drive\files\ folder1 folder2 file1 file2 Long.long.file.name.with.numbers.txt file3 fil
myscript.py \\network-drive\files\Long-long.file.name.with.numbers.txt
文件夹内容如下所示:
\\network-drive\files\
folder1
folder2
file1
file2
Long.long.file.name.with.numbers.txt
file3
file4
import sys
import os.path
if sys.argv[1]: # regardless of this verification, exception happens if argument is not passed
head, tail = os.path.split(sys.argv[1])
print head
print os.listdir(head)
我的脚本应该识别命令行中给定文件的序列号,即返回5(也要计算文件夹;假设文件按名称排序)
Upd。我已停止以下内容:
\\network-drive\files\
folder1
folder2
file1
file2
Long.long.file.name.with.numbers.txt
file3
file4
import sys
import os.path
if sys.argv[1]: # regardless of this verification, exception happens if argument is not passed
head, tail = os.path.split(sys.argv[1])
print head
print os.listdir(head)
listdir
返回的列表不允许我识别什么是文件夹,什么是文件。因此,我无法对它们进行适当分类
from os import listdir
from sys import argv
from os.path import *
print listdir(dirname(argv[1]).index(basename(argv[1]))
但它真的毫无意义,甚至无法想象当你需要它时的用例。有关详细信息,请参见操作系统路径
但它真的毫无意义,甚至无法想象当你需要它时的用例。有关详细信息,请参见
os.path
。看起来应该是这样的:
import os
import glob
import sys
import os.path as path
try:
directory,file = path.split( sys.argv[1] )
def sort_func(fname):
"""
Russian directories , english directories, russian files then english files
although, honestly I don't know how russian files will actually be sorted ...
"""
fullname = path.join(directory,fname)
isRussian = any(ord(x) > 127 for x in fullname)
isDirectory = path.isdir(fullname)
return ( not isDirectory, not isRussian, fullname)
files = sorted( os.listdir(directory), key=sort_func)
print ( files.index(file) + 1 )
except IndexError:
print "oops, no commandline arguments"
看起来应该是这样的:
import os
import glob
import sys
import os.path as path
try:
directory,file = path.split( sys.argv[1] )
def sort_func(fname):
"""
Russian directories , english directories, russian files then english files
although, honestly I don't know how russian files will actually be sorted ...
"""
fullname = path.join(directory,fname)
isRussian = any(ord(x) > 127 for x in fullname)
isDirectory = path.isdir(fullname)
return ( not isDirectory, not isRussian, fullname)
files = sorted( os.listdir(directory), key=sort_func)
print ( files.index(file) + 1 )
except IndexError:
print "oops, no commandline arguments"
您正试图解决几个问题,以及解决方案的几个选项 1-您是否在寻找自然分类的东西,即: 如果是这样,您需要创建一个自然排序方法。如果您对字母数字排序感到满意:
/path/to/folder/
subfolder01/
test1.png
test10.png
test11.png
test2.png
test3.png
然后,标准排序将起作用。根据您对文件的排序方式,结果的索引会有所不同
要从系统中获取目录和文件,您可以使用两种方法中的一种——不能100%确定哪种更快,所以请对它们进行测试。我将把答案分成几块,这样你就可以用最合适的方式把它拼凑起来:
第01部分:初始化
import os
import sys
try:
searchpath = sys.argv[1]
except IndexError:
print 'No searchpath supplied'
sys.exit(0)
basepath, searchname = os.path.split(searchpath)
第二部分:收集文件夹和文件
选项1:os.listdir+os.path.isfile
选项2:操作系统漫游
第03部分:计算指数
选项#1:不排序-从系统中得到的就是你得到的
选项2:字母数字排序
选项3:自然排序
第04部分:记录结果
# if wanting a 1-based answer
index += 1
print index
我不打算详细讨论自然排序,因为这不是问题的一部分——我想这里还有其他论坛,你可以找到关于自然排序的建议。projex.sorting模块是我编写的,可以在这里找到:如果您想了解它的具体实现
这就是结果的不同之处:
>>> import pprint, projex.sorting
>>> files = ['test2.png', 'test1.png', 'test10.png', 'test5.png', 'test11.png']
>>> print files.index('test10.png')
2
>>> print sorted(files).index('test10.png')
1
>>> print sorted(files, projex.sorting.natural).index('test10.png')
3
>>> print files
['test2.png', 'test1.png', 'test10.png', 'test5.png', 'test11.png']
>>> print sorted(files)
['test1.png', 'test10.png', 'test11.png', 'test2.png', 'test5.png']
>>> print sorted(files, projex.sorting.natural)
['test1.png', 'test2.png', 'test5.png', 'test10.png', 'test11.png']
所以当你使用它时,请记住这一点
干杯 您正试图解决两个问题,以及解决方案的两个选项 1-您是否在寻找自然分类的东西,即: 如果是这样,您需要创建一个自然排序方法。如果您对字母数字排序感到满意:
/path/to/folder/
subfolder01/
test1.png
test10.png
test11.png
test2.png
test3.png
然后,标准排序将起作用。根据您对文件的排序方式,结果的索引会有所不同
要从系统中获取目录和文件,您可以使用两种方法中的一种——不能100%确定哪种更快,所以请对它们进行测试。我将把答案分成几块,这样你就可以用最合适的方式把它拼凑起来:
第01部分:初始化
import os
import sys
try:
searchpath = sys.argv[1]
except IndexError:
print 'No searchpath supplied'
sys.exit(0)
basepath, searchname = os.path.split(searchpath)
第二部分:收集文件夹和文件
选项1:os.listdir+os.path.isfile
选项2:操作系统漫游
第03部分:计算指数
选项#1:不排序-从系统中得到的就是你得到的
选项2:字母数字排序
选项3:自然排序
第04部分:记录结果
# if wanting a 1-based answer
index += 1
print index
我不打算详细讨论自然排序,因为这不是问题的一部分——我想这里还有其他论坛,你可以找到关于自然排序的建议。projex.sorting模块是我编写的,可以在这里找到:如果您想了解它的具体实现
这就是结果的不同之处:
>>> import pprint, projex.sorting
>>> files = ['test2.png', 'test1.png', 'test10.png', 'test5.png', 'test11.png']
>>> print files.index('test10.png')
2
>>> print sorted(files).index('test10.png')
1
>>> print sorted(files, projex.sorting.natural).index('test10.png')
3
>>> print files
['test2.png', 'test1.png', 'test10.png', 'test5.png', 'test11.png']
>>> print sorted(files)
['test1.png', 'test10.png', 'test11.png', 'test2.png', 'test5.png']
>>> print sorted(files, projex.sorting.natural)
['test1.png', 'test2.png', 'test5.png', 'test10.png', 'test11.png']
所以当你使用它时,请记住这一点
干杯 请在您的问题中包括您尝试过的内容。正如@IRO bot所指出的,我们需要更多信息来帮助您,并且不会为您编写脚本。在Windows上运行Python脚本有什么问题?浏览文件夹?解析文件名?返回值?印刷品?分类?访问网络共享?@IRO bot,JoeFish,谢谢您的评论。我已经更新了问题。如果需要区分目录和常规文件,请尝试
os.path.isdir()
如何对列表排序?文件夹按字母顺序排列,文件按字母顺序排列?请在问题中包括您尝试过的内容。正如@IRO bot所指出的,我们需要更多信息来帮助您,我们不会为您编写脚本。在Windows上运行Python脚本有什么问题?浏览文件夹?解析文件名?返回值?印刷品?分类?访问网络共享?@IRO bot,JoeFish,谢谢您的评论。我已经更新了问题。如果需要区分目录和常规文件,请尝试os.path.isdir()
如何对列表排序?文件夹按字母顺序排列,文件按字母顺序排列?谢谢。您在键
之前错过了一个括号。这段代码不计算文件夹数。因此,在我的问题示例中,它将返回3而不是5。glob
肯定会统计Unix上的目录,但是os.listdir
可能更好。我将更新。现在它显示ValueError:list.index(x):x不在列表中
。当我在os.listdir(目录):print fl+“:”+str(os.path.isdir(fl))中检查fl的工作方式时,即使对于文件夹,它也会返回False
。希望这次尝试能奏效。(glob
返回完整路径。listdir
只返回文件夹中的文件。因此,我需要将路径信息重新添加到listdir
的结果中)但是如果我使用os.path.isdir(os.path.join(directory,fl)),那么它将返回文件夹的True
。但不知道如何申请