使用Python的rename()函数重命名文件时出现问题,提示 WindowsError: [Error 2] 错误,最初代码如下:

def renameFile(filename):
filePre = "D:\\FileDemo\\Python\\pt.py"
os.rename(filePre, filename)
print os.listdir(filePre) if __name__ == '__main__':
fileNew = "D:\\FileDemo\\Python\\Test.py"
renameFile(fileNew)

  后来经过反复尝试,问题解决~

  rename之前要先用chdir()函数进入到目标文件所在的路径,告诉python编译器要重命名的文件在哪儿,然后才可以修改;

  Python不是可怕的终结者,她其实很幼小,自己找不到文件,需要我们详细又耐心的告诉她该去哪儿找~ 路径通过 os.path.dirname()函数获得:

import os
from nt import chdir def renameF(preName, newName):
chdir(os.path.dirname(preName))
os.rename(preName, newName) if __name__ == '__main__':
filePre = "D:\FileDemo\Python\PT.py"
fileNew = "D:\FileDemo\Python\Test.txt"
renameF(filePre, fileNew)

  代码非常简洁,通过修改filePre,fileNew可以重命名任意文件。

  

解决方案:WindowsError: [Error 2]的更多相关文章

  1. 使用python的subprocess启动windows程序提示WindowsError: [Error 6] The handle is invalid

    代码如下: subp = subprocess.Popen(cwd_path + "test.exe", cwd = cwd_path, shell = True, stdout ...

  2. [Windows Phone] APP上架,遇到错误2001的解决方案。(Error:2001)

    [Windows Phone] APP上架,遇到错误2001的解决方案.(Error:2001) 问题情景 最近在开始玩Windows Phone的开发,开发的过程中虽然有点小挫折,但是参考网络许多前 ...

  3. [解决] python WindowsError: [Error 3]

    python 运行出错 WindowsError: [Error ] : 'D:\\\xe7\x81\xab\xe8\xbd\xa6\xe9\x87\x87\xe9\x9b\x86\xe5\x99\x ...

  4. 使用pytesseract识别验证码,报错WindowsError: [Error 2]

    问题现象: 按照网上的方式进行代码编写,使用pytesseract模块,然后导入指定图片进行解析,报错WindowsError: [Error 2] 问题原因: 源代码里面的路径设置错误,这里有一个坑 ...

  5. 终极报错解决方案:Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger failed with

    遇到这个报错的时候,不要慌 Error:Execution failed for task ':app:processDebugManifest'. > Manifest merger fail ...

  6. Maven报错 解决方案。ERROR: No goals have been specified for this build. You must specify a valid lifecycle phase or a goal in the format <plugin-prefix>:<goal> or <plugin-group-id>:<plugin-artifact-id

    报错: [ERROR] No goals have been specified for this build. You must specify a valid lifecycle phase or ...

  7. redis连接错误3种解决方案System Error MISCONF Redis is configured to save RDB snapshots

    redis连接错误System Error MISCONF Redis is configured to save RDB snapshots, but XX   情况1解决办法: 由于强制停止red ...

  8. [解决方案]docker: Error response from daemon: OCI runtime create failed

    错误原因 在新服务器上安装好docker后,发现无法运行,经常一顿搜索后,发现是docker安装的版本过高,最新版本docker-18.06 的核心好像没有经过充分的测试就发布了. 导致一运行,就提示 ...

  9. 解决方案:Error:Execution failed for task ':app:compileDebugAidl'. > aidl is missing

    也许有朋友最初在用Android Studio创建新工程的时候会碰到这个错误,其实这个问题是由于版本不兼容导致的,解决方法很简单,只需要把编译工具的版本改一下就可以了: 1.选左侧的工程根目录app, ...

随机推荐

  1. Automatic Judge

    Description Welcome to HDU to take part in the second CCPC girls’ competition! A new automatic judge ...

  2. iOS- 显示数据列表最常用的一个控件UITableView

    相信做过iOS的程序员,最熟悉的控件一定少不了UITableView,最常用的控件也一定少不了UITableView! 今天分享一下自己对UITableView的实现大体思路,和整理出来的学习笔记! ...

  3. Qt Creator子目录项目-类似VS解决方案

    版权声明:若无来源注明,Techie亮博客文章均为原创. 转载请以链接形式标明本文标题和地址: 本文标题:Qt Creator子目录项目-类似VS解决方案     本文地址:http://techie ...

  4. 转 linux安装swoole扩展

    linux安装swoole扩展 发表于2年前(2014-09-03 14:05)   阅读(4404) | 评论(3) 7人收藏此文章, 我要收藏 赞2 上海源创会5月15日与你相约[玫瑰里],赶快来 ...

  5. 【Linux】- CentOS 7 安装.NET Core 2.1

    添加dotnet产品Feed 在安装.NET Core之前,您需要注册Microsoft产品Feed. 这只需要做一次. 首先,注册Microsoft签名密钥,然后添加Microsoft产品Feed. ...

  6. 【Linux】- ps 命令

    Linux ps命令用于显示当前进程 (process) 的状态. 语法 ps [options] [--help] 参数: -A     列出所有的行程 -w    显示加宽可以显示较多的资讯 -a ...

  7. perf使用的问题,再看perf record,perf record 设置的采样频率,采样频率是如何体现在

    当perf stat -e branches 是统计 再看perf record,perf record是为了是记录时间发生的时候的调用栈, 在我的测试代码中总共有200,000,000条branch ...

  8. [剑指Offer] 58.对称的二叉树

    题目描述 请实现一个函数,用来判断一颗二叉树是不是对称的.注意,如果一个二叉树同此二叉树的镜像是同样的,定义其为对称的. [思路]递归,关键是isSame函数中的最后一句 /* struct Tree ...

  9. wpf DataGrid加载行号

    <DataGrid Name="tkdg" HorizontalContentAlignment="Center" AutoGenerateColumns ...

  10. 在js中对日期的加减法

    以在某个日期上加减天数来说,其实只要调用Date对象的setDate()函数就可以了,对月份来说,使用setMonth(),具体方法如下:      function addDate(date, da ...