擅长:python、mysql、java
<p>如果两个文件都在同一个文件夹中:</p>
<p><strong>文件</strong></p>
<pre><code>from file_a import var1
print(var1 " is the current path")
userinput = input("Type new directory path here: ")
var1 = userinput
</code></pre>
<p>或者</p>
^{pr2}$
<p>但是,你知道你不能修改另一个文件本身吗?<code>var1</code>将在内存中更改,但如果打开<code>file_a.py</code>,<code>var1</code>将是“/path/to/file”。在</p>
<p><strong>p.S:</strong>一个小建议<code>print(var1 " is the current path")</code>>;<code>print(var1, "is the current path")</code>。在</p>
<p><strong>编辑:</strong>我认为这对你的水平来说是很好的(你这样做就像做家庭作业一样,如果我做一些更有效率的东西或者用一些模块,老师会注意到的)。在</p>
^{3}$
<p>可以在<a href="https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python">this question</a>中找到一个更好的代码(更快,内存使用更少,可能总体上更好)。在</p>