Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/363.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(VS代码环境)中使用.open(“fileName”和“w&“x2B”)函数时,如何更改创建文件的位置?_Python_Visual Studio - Fatal编程技术网

在Python(VS代码环境)中使用.open(“fileName”和“w&“x2B”)函数时,如何更改创建文件的位置?

在Python(VS代码环境)中使用.open(“fileName”和“w&“x2B”)函数时,如何更改创建文件的位置?,python,visual-studio,Python,Visual Studio,如何更改在VS IDE中创建textfile.txt的位置 这是整个项目的结构 Learning Python >.sfdx >.vscode >Debug >Exercise File 我正在编辑的当前文件位于练习文件中的某个位置,但是当textile.txt由Python创建时,它位于学习Python的文件夹中 Learning Python >.sfdx >.vscode >Debug >Exercise File >textfi

如何更改在VS IDE中创建textfile.txt的位置

这是整个项目的结构

Learning Python 
>.sfdx
>.vscode
>Debug
>Exercise File
我正在编辑的当前文件位于练习文件中的某个位置,但是当textile.txt由Python创建时,它位于学习Python的文件夹中

Learning Python 
>.sfdx
>.vscode
>Debug
>Exercise File
>textfile.txt

谢谢你阅读我的问题,希望足够清楚

只需在open函数中指定文件的完整路径即可

f = open(r"C:\Users\deepstop\Documents\textfile.txt", "w+")

字符串前面的r很重要。它的意思是“raw”,防止字符串中的反斜杠被解释为转义字符。

只需在open函数中指定文件的完整路径即可

f = open(r"C:\Users\deepstop\Documents\textfile.txt", "w+")

字符串前面的r很重要。它的意思是“raw”,并防止字符串中的反斜杠被解释为转义字符。

您可以使用自己的python,使用os中的getcwdchdir方法。getcwd返回当前工作目录,chdir用于更改工作目录

import os

current_path = os.getcwd()
print(current_path)

new_path = "/your/path"
os.chdir(new_path)
print(new_path)


您可以使用自己的python来实现这一点,使用os中的getcwdchdir方法。getcwd返回当前工作目录,chdir用于更改工作目录

import os

current_path = os.getcwd()
print(current_path)

new_path = "/your/path"
os.chdir(new_path)
print(new_path)


这也是一种非常有用的方法。我希望我能点击接受这两个答案。干杯这是一种半私人的方式。这也是一种非常有用的方式。我希望我能点击接受这两个答案。干杯这是一种半私人的方式。另一个解决这个问题的好方法。非常感谢你!另一个解决这个问题的好方法。非常感谢你!