Python: FileNotFoundError: [WinError 3] The system cannot find the path specified: ''
You've already read and exhausted the file during the first backup, therefore an empty string is returned for future reads = Invalid path = FileNotFoundError
.
You have to go back to the beginning with seek()
. Place:
src_file.seek(0)
dst_file.seek(0)
after while True:
.
If some files are read-only, it could prevent rmtree()
from working. Define this function:
def remove_readonly(func, path, _):
"Clear the readonly bit and reattempt the removal"
os.chmod(path, stat.S_IWRITE)
func(path)
And then call rmtree()
like this:
shutil.rmtree(dst, onerror=remove_readonly)
Can't solve FileNotFoundError: [WinError 3] The system cannot find the path specified: Error Python
This works (with \\
for windows):
Modifications:
- remove the list comprehension (because it was a bit too long).
- remove
os.chdir(dir)
, no need to change directory.
I was then able to easily inspect each file and run on a test folder.
import os
dir = ("C:\\test")
# check that the folder exists
if os.path.isdir(dir):
print('folder exists')
else:
print('folder does not exist')
exit()
for f in os.listdir(dir):
a = os.path.join(dir, f)
b = os.path.join(dir, f).replace('_', '').replace(' ', '').lower()
os.rename(a, b)
print('file names updated')
Here are the results:
file system before:
file system after:
Python [WinError 3] The system cannot find the path specified:
This happens since you're changing the working dir with os.chdir
and not changing it back to where it was at the end of your run, while also using relative paths instead of absolute paths.
You could add old_working_dir = os.os.getcwd()
at the start of your file and os.chdir(old_working_dir)
at the end to return to where you were and have it run again as intended, but that's a bit hacky for my taste.
A much better solution would be to not use chdir
at all, and make your script work with absolute paths instead of relative ones if possible.
Also, check out pathlib
for many easier ways to deal with file paths.
Getting the following Error: FileNotFoundError: [WinError 3] The system cannot find the path specified:
You are reading the content of the .csv file in the variable data_location
. Then, you are calling os.listdir()
using the content of the .csv file, and this is not a string directory but a data frame so it will throw an error.
The variable data_location
must be a string containing the path of the directory that you are trying to list its files.
Related Topics
Using a Global Variable With a Thread
How to Read Pdf Files One by One from a Folder in Python
Setting Matplotlib Colorbar Range
How to Repeat Each Test Multiple Times in a Py.Test Run
Valueerror: X and Y Must Be the Same Size
How to Check Url Change With Selenium in Python
Split String in a Spark Dataframe Column by Regular Expressions Capturing Groups
_Tkinter.Tclerror: No Display Name and No $Display Environment Variable
Most Efficient Way to Construct Similarity Matrix
How to Test If a List Contains Another List as a Contiguous Subsequence
How to Enable Autocomplete (Intellisense) for Python Package Modules
In Python, How to Find the Vowels in a Word
Render_Template in Python-Flask Is Not Working
Dividing Each Row by the Previous One
Pyspark - Pass List as Parameter to Udf
Python/Pandas: Convert Month Int to Month Name
I Need to Code a 1 22 333 4444 Pattern in Python With While Loops