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.
Related
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
This question already has answers here:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
File Not found when i should already copy that file to the directory
I already tried to copy that file in various folders like Include etc
import numpy as np
import collections
a=np.genfromtxt('Desktop\a.csv',delimiter=',',dtype=str)
print(a)
All the data that is in the file
\a is an escape sequence like \n. In this case, it is the BEL character. Escape the backslash (\\a), use a raw string to suppress escape code translation (r'Desktop\a.csv') or use forward slashes.
Note you are also using a relative path, meaning you have to run the code from the directory in which "Desktop" is a sub-directory. Use a full path to make sure you get it on the desktop, which will include your username like 'c:/users/garauv/desktop/a.csv' or whatever your real username is.
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:
How should I write a Windows path in a Python string literal?
(5 answers)
Closed 7 months ago.
How can I create a string that represents a Windows Path? I need to add a file that is generated dynamically to the end of the path as well. I have tried using raw strings but I can't seem to figure it out. Here's what I am trying to accomplish:
filename = "test.txt"
path = 'C:\Path\To\Folder\' + filename
Error I am seeing is: SyntaxError: EOL while scanning string literal
Sorry if this has been asked before I have tried looking at a few other SO questions and everyone reccomends using os.path.join but the problem there is I need to build a string, this code will not be running on a windows machine... Does that make a difference. Any help will be greatly appreciated!
You have a string literal, not a raw string. A raw string is a string literal with an r before it:
path = r'C:\Path\To\Folder\' + filename
Edit: Actually, that doesn't work because raw strings can't end with backslashes. If you still want to use a raw string, you could do one of the following:
path = r'C:\Path\To\Folder' + '\\' + filename
path = r'C:\Path\To\Folder\{}'.format(filename)
You could also double the backslashes:
path = 'C:\\Path\\To\\Folder\\' + filename
Yours didn't work because a backslash is a special character. For example, \n is not a backslash and an n; it is a new line. \' means to treat ' as a literal character instead of making it end the string. That means that the string does not end there, but you don't end it later in the line either. Therefore, when Python gets to the end of the line and you still haven't closed your string, it has an error. It is still better to use os.path.join(), though:
path = os.path.join(r'C:\Path\To\Folder', filename)
filename = "test.txt"
path = os.path.join(r'C:\Path\To\Folder',filename)
although really python works fine using / as a file separator on any OS
This question already has answers here:
Parsing Command line arguments in python which has spaces
(4 answers)
Closed 7 years ago.
I am trying to pass source and destination path from the command line as below
python ReleaseTool.py -i C:\Users\Abdur\Documents\NetBeansProjects\Exam System -o C:\Users\Abdur\Documents\NetBeansProjects\Release
but it is throwing error
WindowsError: [Error 3] The system cannot find the path specified: ''
due to 'Exam System' which has a space between.
Please suggest how to handle this.
Cause
Long filenames or paths with spaces are supported by NTFS in Windows NT. However, these filenames or directory names require quotation marks around them when they are specified in a command prompt operation. Failure to use the quotation marks results in the error message.
Solution
Use quotation marks when specifying long filenames or paths with spaces. For example, typing the following at the command prompt
copy c:\my file name d:\my new file name
results in the following error message:
The system cannot find the file specified.
The correct syntax is:
copy "c:\my file name" "d:\my new file name"
Note that the quotation marks must be used.