使用 Python os.makedirs 在 Windows 中创建可写目录

发布于 2024-12-12 20:14:31 字数 709 浏览 3 评论 0原文

我需要创建一个子目录(如果它尚不存在),然后将一些文件复制到其中。但是,每当我尝试时,都会收到“权限被拒绝”错误。我尝试过 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

橘虞初梦 2024-12-19 20:14:31

我认为 shutdown.copyfile 需要目标文件的完整文件名,而不仅仅是目录。

所以

shutil.copyfile(oldfile, os.path.join(deleted, newpagename))

应该这样做。

I think that shutil.copyfile needs the complete file name of the destination file, not just the directory.

So

shutil.copyfile(oldfile, os.path.join(deleted, newpagename))

should do the trick.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文