Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/database/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用python创建文件和文件夹_Python - Fatal编程技术网

使用python创建文件和文件夹

使用python创建文件和文件夹,python,Python,我是python新手,我尝试创建一个文件,将当前脚本移动到该文件中并运行它,但我一直遇到同样的问题,它会创建一个文件夹,移动文件,但当我打开脚本时,它会创建一个新文件夹并移动文件。因此,最终的文件目录是:“C:somefile\someotherfile\newfile\newfile\newfile\script.py” 请帮忙 代码: 编辑: 当给出的答案不起作用时,我花了两天的时间试图自己解决这个问题(以及我发帖前的一天)。我终于找到了答案 import os curpath = __fi

我是python新手,我尝试创建一个文件,将当前脚本移动到该文件中并运行它,但我一直遇到同样的问题,它会创建一个文件夹,移动文件,但当我打开脚本时,它会创建一个新文件夹并移动文件。因此,最终的文件目录是:“C:somefile\someotherfile\newfile\newfile\newfile\script.py”
请帮忙
代码:

编辑: 当给出的答案不起作用时,我花了两天的时间试图自己解决这个问题(以及我发帖前的一天)。我终于找到了答案

import os
curpath = __file__.lower()
abspath=os.path.abspath(curpath)
newpath = ('.\\example folder\\')
notpath = '..\\example folder\\'
if not os.path.exists(newpath) and not os.path.exists(notpath):
    os.makedirs(newpath)
    os.rename(abspath, '.\\example folder\\test.py')
curpath!=('..\\Example Folder\\Test.py')
将始终计算为true,因为
curpath
被设置为
curpath=('.\\Test.py')
且从未更改。这些是字符串,而不是文件位置,因此
'.\\Test.py'
永远不会等于
'C:\\\\Test.py'

你应该做的是:

curpath=('.\\Test.py')
替换为
curpath=\uuuuu文件\uuuuu.lower()

替换

if not os.path.exists(newpath) and curpath != ('..\\Example Folder\\Test.py'):

如果没有os.path.exists(newpath)和curpath!='c:\\\\example folder\\test.py'.lower():
希望这有帮助

curpath!=('..\Example Folder\Test.py')将始终计算为 true,因为curpath设置为curpath=('.\Test.py'),并且从不 改变。这些是字符串,不是文件位置,因此“.\Test.py”将 永远不等于'C:\\Test.py'

你应该做的是:

用curpath=文件替换curpath=('.\Test.py')

替换

如果不存在os.path.exists(新路径)和curpath!=('..\示例 文件夹\Test.py'):带有

如果不存在os.path.exists(newpath)和curpath!='c:\\example folder\test.py'.lower():

对于“c:\(输入完整abs路径)”我可以执行以下操作:

path='.'
abpath=os.path.abspath(path)
and curpath != abpath + '\\example folder\test.py'.lower():

如果没有,那么idk如何做作为idk我如何获得完整的abs路径,因为有人可以在文档中拥有它,而其他人可以在下载中拥有它

curpath!=('..\\Example Folder\\Test.py')
将始终计算为true,因为curpath被设置为
curpath=('.\\Test.py')
,并且永远不会更改。这些是字符串,而不是文件位置,因此
'.\\Test.py'
永远不会等于
'C:\\\\\Test.py'
,那么如何确保它不会创建一堆文件夹呢?设置
curpath=\uu file\uu.lower()
并将if改为
和curpath!='c:\\\\example folder\\test.py'.lower():
。我不确定我是否理解了您的意图。你所说的“当前脚本”就是正在运行的脚本?它会复制自身并永远调用自身?不幸的是,这将不起作用(以及在最后一行中丢失的转义“\”),它也将始终计算为true,因为
os.path.abspath('.')
\uu file\uuuu.lower()
,这意味着
curpath
将始终等于
abspath
,所以它永远不会等于
abspath+
。这个特定代码的问题是,您必须手动输入目录,因为如果没有手动输入,代码无法知道您是否希望它复制出该目录
if not os.path.exists(newpath) and curpath != 'c:\\<enter full abs path>\\example folder\\test.py'.lower():
path='.'
abpath=os.path.abspath(path)
and curpath != abpath + '\\example folder\test.py'.lower():