This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
I want to work with paths in Windows in Python 3.3, but I have an error:
FileNotFoundError: [Errno 2] No such file or directory:
'E:\\dir\\.project'
The problem is the double backslash. I read the solution using r.
def f(dir_from):
list_of_directory = os.listdir(dir_from)
for element in list_of_directory:
if os.path.isfile(os.path.join(dir_from, element)):
open(os.path.join(dir_from, element))
f(r'E:\\dir')
I have this error again
FileNotFoundError: [Errno 2] No such file or directory:
'E:\\dir\\.project'
os.path.normpath(path) doesn't solve my problem.
What am I doing wrong?
If you are using a raw-string, then you do not escape backslashes:
f(r'E:\dir')
Of course, this problem (and many others like it) can be solved by simply using forwardslashes in paths:
f('E:/dir')
Changing '\\' for '/' worked for me. I created a directory named 'a' in C:/ for this example.
>>> (Python interpreter)
>>> import os
>>> os.path.isdir('C:/a/)')
>>> True
>>> os.path.isfile('C:/a/)')
>>> False
Related
This question already has answers here:
How can I create files on Windows with embedded slashes, using Python?
(3 answers)
Closed 2 years ago.
When I run the following code, instead of creating a text file in my working directory with the name '03/08/2020.txt', it generates an error FileNotFoundError: [Errno 2] No such file or directory: '03/08/2020.txt'. As far as I think, it is just because of the slashes.
But anyhow, I want to create a text file with slashes because this thing is a part of a large code and I have to work with dates (for attendance purposes).
dates = ['03/08/2020', '1', '2', '3']
def test(alist):
myfile = open(alist[0])+'.txt', 'w')
for i in alist:
myfile.write(f"{i}\n")
myfile.close()
test(dates)
is there a way yo handle this issue?
As jdaz said, you can instead use "03-08-2020.txt"
This is because on windows, you can't add to the following characters to your file names:
\ / : * ? " < > |
If you try to rename a file with one of those characters, you'll see a message saying you can't do so.
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
Can anyone explain how to work with paths with python on windows.
the path is given as parameter
path = 'C:\Data\Projects\IHateWindows\DEV_Main\Product\ACSF\Dev\DEV\force.com\src\aura'
Im trying to get the content of the folder but is not a valid path
is reading the path as:
'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\x0corce.com\\src\x07ura'
trying some solutions...
for f in listdir(path.replace("\\", "\\\\")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\\\Data\\\\Projects\\\\Prd_Development\\\\DEV_Main\\\\Product\\\\ACSF\\\\Dev\\\\DEV\x0corce.com\\\\src\x07ura'
for f in listdir(path.replace("\\", "/")): print (f)
OSError: [WinError 123] The filename, directory name, or volume label syntax is incorrect: 'C:/Data/Projects/Prd_Development/DEV_Main/Product/ACSF/Dev/DEV\x0corce.com/src\x07ura'
EDIT:
Solution
path = path.replace("\a", "\\a").replace("\f", "\\f")
https://docs.python.org/2.0/ref/strings.html
A single Backslash is a escape character in Python. Therefore you might need to use double Backslashes or a forward slash:
path = 'C:\\Data\\Projects\\IHateWindows\\DEV_Main\\Product\\ACSF\\Dev\\DEV\\force.com\\src\\aura'
or
path = 'C:/Data/Projects/IHateWindows/DEV_Main/Product/ACSF/Dev/DEV/force.com/src/aura'
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 4 years ago.
I want to iterate over a number of files in the L: drive on my computer in a folder called 11109. This is my script:
for filename in os.listdir('L:\11109'):
print(filename.split('-')[1])
However the error message comes back as :
File "L:/OMIZ/rando.py", line 12, in <module>
for filename in os.listdir('L:\11109'):
FileNotFoundError: [WinError 3] The system cannot find the path specified:
'L:I09'
Its reading the L:\ 11109 fine but the error message said the path specified is L:I09?
You need to use raw strings or escape the backslash, otherwise \111 is resolved to I:
a = 'L:\11109'
print(a) # shows that indeed 'L:I09'
b = r'L:\11109'
print(b) # prints 'L:\11109'
c = 'L:\\11109' # will be understood correctly by open()
To solve this you can do like this
for filename in os.listdir('L:/11109'):
print(filename.split('-')[1])
This question already has answers here:
Why do backslashes appear twice?
(2 answers)
Closed 6 years ago.
I have a requirement in which i have to specify a file path of an object (say 'abcd.jpg'). I am able to find the path with os module in python. But it contains '\\' as a separator. How to convert this separator to '\'.
>>> import os
>>> a = os.getcwd() + '\\' + 'abcd.jpg'
>>> a
'C:\\Python27\\abcd.jpg'
In my script i am only allowed to write the path name as 'C:\Python27\abcd.jpg'
Just do print a and you should be fine. What you are doing is actually the repr of the string.
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 6 months ago.
So I have a script that needs to print to a file in a different directory. I give that absolute path and python doesn't like it.
Here's where the file is located:
C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt
(I know, long path, but QT plotter makes the file names really long)
I typed:
textfile = open('C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt', 'w')
And I get this error:
IOError: [Errno 22] invalid mode ('w') or filename:
I've read that I can use relative paths, but I am unsure how to give it a relative path with so many directories to go through.
Thanks!
The problem is that python is interpreting the backslashes in your path as escape sequences:
>>> 'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\x08uild-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
Notice that both \b and \n get translated to something else. Use a "raw" string instead:
>>> r'C:\Users\Owner\Documents\Senior_design\QT_Library\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\numbers.txt'
'C:\\Users\\Owner\\Documents\\Senior_design\\QT_Library\\build-TransmitterPlot-Desktop_Qt_5_0_2_MSVC2010_32bit-Debug\\numbers.txt'
I believe this answer here may be of help.
Essentially, your backslashes are causing issues.