Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.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/9/java/309.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,我正在做一项学校作业,我必须从用户那里获取输入并将其保存到文本文件中。 我的文件结构类似于: -客户登记簿 -客户ID -.txt文件1-5 它可以保存在python文件夹中,我可以将文件夹设置为: os.makedirs("Customer register/Customer ID") 我的问题是,当我不知道目录时,如何在目录中设置文本文件要存储的路径?因此,无论程序在哪里运行,它都会保存在我创建的“客户ID”文件夹中(但程序是在计算机上运行的)? 另外,我如何在windows和mac上都能

我正在做一项学校作业,我必须从用户那里获取输入并将其保存到文本文件中。 我的文件结构类似于: -客户登记簿 -客户ID -.txt文件1-5

它可以保存在python文件夹中,我可以将文件夹设置为:

os.makedirs("Customer register/Customer ID")
我的问题是,当我不知道目录时,如何在目录中设置文本文件要存储的路径?因此,无论程序在哪里运行,它都会保存在我创建的“客户ID”文件夹中(但程序是在计算机上运行的)? 另外,我如何在windows和mac上都能做到这一点

我还希望程序能够执行多次,并检查文件夹是否存在,如果已经存在,则保存到“Customer ID”文件夹。有办法吗

编辑: 这是我尝试使用的代码:

try:
    dirs = os.makedirs("Folder")
    path = os.getcwd()
    os.chdir(path + "/Folder")
    print (os.getcwd())
except:
    if os.path.exists:
        path = os.getcwd()
        unique_filename = str(uuid.uuid4())
        customerpath = os.getcwd()
        os.chdir(customerpath + "/Folder/" + unique_filename)
我可以创建一个文件夹并更改目录(在“try”中的所有内容都可以按照我的要求工作)。 创建此文件夹时,我要创建第二个文件夹,其中包含随机生成的文件夹名(用于保存客户文件)。我不能以同样的方式让它工作。 错误: FileNotFoundError:[WinError 2]系统找不到指定的文件:“C:\Users\48736\PycharmProjects\tina/Folder/979b9026-b2f6-4526-a17a-3b53384f60c4”

编辑2:

try:
    os.makedirs("Folder")
    path = os.getcwd()
    os.chdir(path + "/Folder")
    print (os.getcwd())

except:
    if os.path.exists:
        path = os.getcwd()
        os.chdir(os.path.join(path, 'Folder'))

print(os.getcwd())

def userId(folderid):
try:
    if not os.path.exists(folderid):
        os.makedirs(folderid)
except:
    if os.path.exists(folderid):
        os.chdir(path + "/Folder/" + folderid)
userId(str(uuid.uuid4()))


print(os.getcwd())
所以我现在可以创建一个文件夹,将目录更改为我创建的文件夹,并在该文件夹中创建一个具有唯一文件名的新文件夹。 但我无法再次将目录更改为具有唯一文件名的文件夹。 有什么建议吗

我试过:

os.chdir(path + "/Folder/" + folderid)
os.chdir(path, 'Folder', folderid)
os.chdir(os.path.join(path, 'Folder', folderid))

但是仍然只是停留在:C:\Users\47896\PycharmProjects\tina\Folder

您可以在create directory命令中使用相对路径,即

os.makedirs("./Customer register/Customer ID")
在项目根目录(=主调用者所在的位置)中创建文件夹,或

os.makedirs(“../Customer register/Customer ID”)
位于父目录中

当然,您可以根据需要遍历文件树

有关问题中提到的具体选项,请参见
makedirs
文档,网址为,这里是解决方案

import os
import shutil
import uuid

path_on_system = os.getcwd()  # directory where you want to     save data
path = r'Folder' # your working directory  

dir_path  = os.path.join(path_on_system, path)  


if not os.path.exists(dir_path):
    os.makedirs(dir_path)

file_name = str(uuid.uuid4())  # file which you have created 

if os.path.exists(file_name) and os.path.exists(dir_path):
    shutil.move(file_name,os.path.join(dir_path,file_name))
else:
      print(" {} does not exist".format(file_name))

根据您的问题判断,您知道他们需要将文件存储在
的“客户注册/客户ID”
中,这样您就可以将其预先添加到您编写的任何文件中。或者,使用
os.chdir
设置您的工作方向如果我正确地了解了您的问题,
os.makedirs(客户注册+'/'+客户ID)
,在调用之前填充变量。要检查文件夹是否在那里,您可以使用os.isdir('directory To check')。@TinaOlsen与您分享解决此问题的代码problem@prashantrana问题是我不知道目录的完整路径,因为程序将由我的老师在他的计算机上打开。我知道我计算机上的完整路径,但我希望这是任何运行该程序的计算机上的工作。@TinaOlsen使用relative查看Mikulas的答案paths@TinaOlsen这会奏效的。它将在程序运行的同一目录中创建“Customer register/Customer ID”。@MichalPolovka我发现mikulas使用“/”或“../”创建路径的答案是错误的。您需要首先验证该路径是否存在。我知道如何创建目录,我不知道如何将目录路径更改为我在其他计算机上创建的目录。我可以创建我想要的目录,但我不知道如何将当前目录路径设置为“任意”计算机。换句话说,如果您在计算机上运行此程序,如何将其保存到计算机上的所需路径?当我不知道你的目录路径时。你还应该注意,当我们有
OS.path.join
@AzatIbrakov时,使用依赖操作系统的分隔符是个坏主意。当使用
/
@MichalPolovka时,Python会自动替换有效的路径分隔符。你确定Python会这样做吗?据我所知,Python将路径传递到操作系统。幸运的是,Windows也乐于接受
/
。然而,
os.path.join()
是一种很好的风格。