python oserror errorno 39_python - OSError: [Errno 2] No such file or directory: '39'

在Python中尝试创建目录时遇到OSError:[Errno 2] No such file or directory的问题。问题出在`os.chdir()`上,当改变工作目录后,试图在可能不存在的路径下创建新目录。解决方案是使用一个基础目录并确保所有路径操作都在该基础目录下进行。
摘要由CSDN通过智能技术生成

-1

I have the following piece of code to make a directory on particular conditions.

def create_analysis_folder(self, analysis_id, has_headers):

path = None

if not os.path.exists(analysis_id):

os.makedirs(analysis_id)

os.chdir(analysis_id)

if has_headers == False:

path = os.getcwd() + '/html'

return path

else:

os.makedirs('html')

os.chdir('html')

shutil.copy("../../RequestURL.js", os.getcwd())

return os.getcwd()

Upon execution this gives me an error in line

os.makedirs(analysis_id)

The error says OSError: [Errno 2] No such file or directory: '39'. But I am in the processor creating a directory then why am I getting an error like this.

python

|

this question

edited Dec 29 '14 at 10:14

J.F. Sebastian 205k 51 408 624 asked Dec 29 '14 at 9:20

station 1,552 9 28 54      It seems your 'analysis_id' is number. Try converting it in string –

Ajay Tanpure Dec 29 '14 at 9:24 1   What is the traceback? Don't use

chdir, after executing

create_analysis_folder you never know, in which directory you are actually. –

Daniel Dec 29 '14 at 9:47

|

1 Answers

1

---Accepted---Accepted---Accepted---

The problem is your chdir as I already stated in my comment. Here's what happens:d this error and what to do answer 1 >>---Accepted---Accepted---Accepted--- Have you noticed that you don't get the error if you run python ./script.py instead of python script.py This is because sys.argv[0] will read ./script.py in the former case, which gives os

>>> os.makedirs('a/b/c') # create some directories

>>> os.chdir('a/b/c') # change into this directory

>>> os.rmdir('../c') # remove the current directory

>>> os.makedirs('z') # trying to create a directory in a non-existing directory

Traceback (most recent call last):

File "", line 1, in

File "/.../python2.7/os.py", line 157, in makedirs

mkdir(name, mode)

OSError: [Errno 2] No such file or directory: 'z'

The correct way, to handle such a problem is:

BASE_DIR = os.getcwd() # or any other path you want to work with

def create_analysis_folder(self, analysis_id, has_headers):

if not os.path.exists(os.path.join(BASE_DIR, analysis_id)):

os.makedirs(os.path.join(BASE_DIR,analysis_id))

path = os.path.join(BASE_DIR, analysis_id, 'html')

if has_headers:

os.makedirs(path)

shutil.copy(os.path.join(BASE_DIR, "RequestURL.js"), path)

return path

|

this answer answered Dec 29 '14 at 11:35

Daniel 25.1k 4 19 48

|

ule attempts to open a second program and cashes #!/usr/bin/pythonfrom sys import argv, exitfrom subprocess import Popen, PIPEfrom collections import defaultdictfrom os import path, removedef main():if len(argv) != 4 or not argv[2] i

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值