使用 Python os.makedirs 在 Windows 中创建可写目录
我需要创建一个子目录(如果它尚不存在),然后将一些文件复制到其中。但是,每当我尝试时,都会收到“权限被拒绝”错误。我尝试过 chmod,使用 777 以及 stat.S_IWRITE,我尝试过 os.system('attrib -r),但没有任何效果。谁能帮我解决这个问题吗?我知道该网站上有一个类似的问题,但它说使用 chmod,这对我不起作用。
这是我的代码:
beginpath = "C:\Users\foo"
fullpath = os.path.join(beginpath, foldername)
print fullpath
fullpath = fullpath.replace('\n', '')
##create a folder to hold the deleted files
deleted = os.path.join(fullpath, "Deleted")
print deleted
if not os.path.exists(deleted):
os.makedirs(deleted)
os.chmod(deleted, stat.S_IWRITE)
print "created"
##do some other processing here
oldfile = os.path.join(fullpath, newpagename)
shutil.copyfile(oldfile, deleted)
I need to create a subdirectory if it doesn't yet exist, then copy some files into it. However, whenever I try, I get a Permission denied error. I've tried chmod, with 777 as well as stat.S_IWRITE, I've tried os.system('attrib -r), but nothing works. Can anyone help me work this one out? I know there is a similar question on the site, but it says to use chmod, which isn't working for me.
Here's my code:
beginpath = "C:\Users\foo"
fullpath = os.path.join(beginpath, foldername)
print fullpath
fullpath = fullpath.replace('\n', '')
##create a folder to hold the deleted files
deleted = os.path.join(fullpath, "Deleted")
print deleted
if not os.path.exists(deleted):
os.makedirs(deleted)
os.chmod(deleted, stat.S_IWRITE)
print "created"
##do some other processing here
oldfile = os.path.join(fullpath, newpagename)
shutil.copyfile(oldfile, deleted)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为 shutdown.copyfile 需要目标文件的完整文件名,而不仅仅是目录。
所以
应该这样做。
I think that
shutil.copyfile
needs the complete file name of the destination file, not just the directory.So
should do the trick.