Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 下载文件时,google chrome如何自动重命名文件?_Python_File_Google Chrome_Rename - Fatal编程技术网

Python 下载文件时,google chrome如何自动重命名文件?

Python 下载文件时,google chrome如何自动重命名文件?,python,file,google-chrome,rename,Python,File,Google Chrome,Rename,我想获取文件的扩展名?例如: import os print os.path.splitext('testit.doc') """>>>('testit', '.doc')""" 但是当我使用下面的示例时,它不起作用 import os print os.path.splitext('testid.tar.gz') """>>>('testit.tar', '.gz')""" 我看到Chrome可以在位置上有同名文件时自动重命名文件。它会添加(1)或(n)

我想获取文件的扩展名?例如:

import os
print os.path.splitext('testit.doc')
""">>>('testit', '.doc')"""
但是当我使用下面的示例时,它不起作用

import os
print os.path.splitext('testid.tar.gz')
""">>>('testit.tar', '.gz')"""
我看到Chrome可以在位置上有同名文件时自动重命名文件。它会添加(1)或(n)。我想知道它是怎么做的! 有人能告诉我吗?

我认为它使用了一系列著名的文件扩展名,您也可以这样做,您有很多方法(可能比我的解决方案性能更好,例如使用正则表达式),但这是一个非常简单的解决方案:

import os

known_extensions = ['.tar.gz', '.tar.bz2']
def splitext(file_name):
    file_name = file_name.strip()

    for ex in known_extensions:
        if file_name[-len(ex):] == ex:
            return file_name[:-len(ex)], ex

    return os.path.splitext(file_name)

幸运的是,chromium是开源的,因此您可以查看文档丰富的代码。好啊我发现它:

RenameAndUniquify
是:

void DownloadFileImpl::RenameAndUniquify(
    const base::FilePath& full_path,
    const RenameCompletionCallback& callback) {
  DCHECK(BrowserThread::CurrentlyOn(BrowserThread::FILE));

  base::FilePath new_path(full_path);

  int uniquifier =
      file_util::GetUniquePathNumber(new_path, FILE_PATH_LITERAL(""));
  if (uniquifier > 0) {
    new_path = new_path.InsertBeforeExtensionASCII(
        base::StringPrintf(" (%d)", uniquifier));
  }

...

}
insertbeforepension
调用您感兴趣的
ExtensionSeperatorPosition

//查找将扩展名与其余扩展名分开的“.”的位置
//文件名的名称。该位置相对于BaseName(),而不是value()。
//这样,当
//最右边的扩展组件是常见的双扩展(gz、bz2、Z)。
//例如,foo.tar.gz或foo.tar.Z的扩展组件为
//分别为'.tar.gz'和'.tar.Z'。如果找不到目标,则返回NPO
//分机。
StringType::大小\类型扩展分离位置(常量StringType和路径){
//特例“.”和“.”
如果(路径==文件路径::kCurrentDirectory | |路径==文件路径::kParentDirectory)
返回StringType::npos;
常量StringType::大小\u类型最后\u点=
rfind(FilePath::kExtensionSeparator);
//没有扩展名,或者扩展名是整个文件名。
if(last_dot==StringType::npos | | last_dot==0U)
返回最后一个点;
常量StringType::大小\类型倒数第二个\点=
rfind(FilePath::kExtensionSeparator,last_dot-1);
常量StringType::大小\类型最后一个\分隔符=
查找(FilePath::kSeparators,last_dot-1,
阵列化(文件路径::kSeparators)-1);
如果(倒数第二个点==StringType::npos||
(最后一个分隔符!=StringType::npos&&
倒数第二个点<最后一个分隔符){
返回最后一个点;
}
对于(大小i=0;i
它如何处理像“I.love.dots.some.extension”这样的文件名?我猜它只是有一个“多扩展名”列表(比如
.tar.bz2
.tar.gz
,等等),并简单地检查它是否必须以特殊方式处理文件名。另一种可能是执行
filename.split('.',1)
,但这与
I.love.dots
样式的文件名不同。我不知道它们是如何执行的,或者它们是否有非常简单的方法。基于第一个文件(x.tar(1.tar.gz),看起来他们尝试了两次检查文件扩展名。如果您创建了一个名为“mytest.filename.gz”或“mytest.filename.rar”的文件,您可以尝试测试它。如果结果是“mytest.filename(1.gz)”或“mytest.filename(1.rar”,那么它看起来像是有一些已知的扩展名匹配。否则,他们可能会有一个简单的“两次扩展测试”方法:)我使用“mytest.filename.gz”测试它,并得到“mytest.filename(1.gz)”。另外,我使用“abc.abc”测试它,得到了“abc(1.abc)”。它看起来像已知的扩展匹配@巴库留-你是对的。我只是检查了它到底是怎么做到的。
// Find the position of the '.' that separates the extension from the rest
// of the file name. The position is relative to BaseName(), not value().
// This allows a second extension component of up to 4 characters when the
// rightmost extension component is a common double extension (gz, bz2, Z).
// For example, foo.tar.gz or foo.tar.Z would have extension components of
// '.tar.gz' and '.tar.Z' respectively. Returns npos if it can't find an
// extension.
StringType::size_type ExtensionSeparatorPosition(const StringType& path) {
  // Special case "." and ".."
  if (path == FilePath::kCurrentDirectory || path == FilePath::kParentDirectory)
    return StringType::npos;

  const StringType::size_type last_dot =
      path.rfind(FilePath::kExtensionSeparator);

  // No extension, or the extension is the whole filename.
  if (last_dot == StringType::npos || last_dot == 0U)
    return last_dot;

  const StringType::size_type penultimate_dot =
      path.rfind(FilePath::kExtensionSeparator, last_dot - 1);
  const StringType::size_type last_separator =
      path.find_last_of(FilePath::kSeparators, last_dot - 1,
                        arraysize(FilePath::kSeparators) - 1);

  if (penultimate_dot == StringType::npos ||
      (last_separator != StringType::npos &&
       penultimate_dot < last_separator)) {
    return last_dot;
  }

  for (size_t i = 0; i < arraysize(kCommonDoubleExtensions); ++i) {
    StringType extension(path, penultimate_dot + 1);
    if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensions[i]))
      return penultimate_dot;
  }

  StringType extension(path, last_dot + 1);
  for (size_t i = 0; i < arraysize(kCommonDoubleExtensionSuffixes); ++i) {
    if (LowerCaseEqualsASCII(extension, kCommonDoubleExtensionSuffixes[i])) {
      if ((last_dot - penultimate_dot) <= 5U &&
          (last_dot - penultimate_dot) > 1U) {
        return penultimate_dot;
      }
    }
  }

  return last_dot;
}