简体   繁体   English

Python os.chdir()不会更改目录

[英]Python os.chdir() not changing directory

So, I am following a simple tutorial "Python Tutorial: Automate Parsing and Renaming of Multiple Files" and am already encountering a problem where os.chdir() is not working. 因此,我正在遵循一个简单的教程“ Python教程:自动分析和重命名多个文件”,并且已经遇到了os.chdir()无法正常工作的问题。 I am on a Windows 10 system, running python 3.6, and I have tried using both my regular terminal (which has cygwin installed) and bash on ubuntu on Windows. 我在运行python 3.6的Windows 10系统上,并且尝试在Windows的ubuntu上同时使用常规终端(已安装cygwin)和bash。

Here is the code: 这是代码:

import os

print(os.getcwd())

os.chdir('c:/Users/Michelle Kaiser/Desktop/Lab_Progs/PI3Kalpha')

print(os.getcwd())

Here is the reg terminal: 这是reg终端:

C:\Users\Michelle Kaiser\Desktop\Lab_Progs>python rename.py
C:\Users\Michelle Kaiser\Desktop\Lab_Progs

C:\Users\Michelle Kaiser\Desktop\Lab_Progs>`

The path that it is returning corresponds to the folder my program is located in. I have moved the program 3 times to verify this. 它返回的路径对应于我的程序所在的文件夹。我已将该程序移动了3次,以验证这一点。 Also, it's obviously returning a path only once, so it's probably not responding to the 2 print statements. 另外,它显然只返回一次路径,因此它可能不响应2个print语句。

Here is the bash terminal: 这是bash终端:

mkaiser@ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$ python rename.py
/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs
mkaiser@ZIPPY:/mnt/c/Users/Michelle Kaiser/Desktop/Lab_Progs$

I also tried running the code with os.path.exists() , which did not change the output on either terminal. 我还尝试使用os.path.exists()运行代码,但未更改任何终端上的输出。 I have definitely double checked that I am saving my program file from one test to the next. 我肯定已经仔细检查过,我正在将程序文件从一个测试保存到另一个测试。 Thanks. 谢谢。

I've been trying to change a file that has no whitespace. 我一直在尝试更改没有空格的文件。

It seems like this person has a similar problem: 似乎此人有类似的问题:

Python reading whitespace-separated file lines as separate lines Python将以空格分隔的文件行读取为单独的行

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

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