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.
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'
I am trying to write code that takes 2 numbers in a text file and then divides them, showing the answer as a top heavy fraction. I have gotten the fractions part to work when I am inputting my own values in the program, but i cannot get the program to recognise the text file. I have tried putting them in the same directory and putting the full system path of the file, but nothing so far has worked. right now I am just trying to get the contents of the file to print.
with open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w') as f:
for line in f:
for word in line.split():
print(word)
I will then assign the 2 values to x and y, but I get this error:
Traceback (most recent call last):
File "C:\Python34\divider.py", line 2, in <module>
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\\ProgramData\\Microsoft\\Windows\\Startmenu\\Programs\\Python 3.4\topheavy.txt'
Two things:
When working with paths that contain backslashes, you either need to use two backslashes, or use the r'' form to prevent interpreting of escape sequences. For example, 'C:\\Program Files\\...' or r'C:\Program Files\...'.
Your error shows this: \\Startmenu\\. It appears that a space is missing between "Start" and "menu", despite the fact that the open line seems to have the right path.
Note: that the \topheavy.txt in your path is probably getting converted to <tab>opheavy.txt too. That's why there aren't two backslashes in front of it in the traceback.
You are using a "\" separator which is probably getting escaped somewhere (like that \t near the end. That's the Windows path separator, but also used as a string escape.
You can double up the "\" as "\". Easiest however is to prepend an r at the beginning to ignore .
r"C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt"
Skip the recommendation to use / instead, you are not on Unix and there is no reason Python can't accommodate Windows, as long as you remember to take care about "\" also being an escape. Using r' at start also allows you to copy/paste from the string into another program or vice-versa.
also, it wouldn't hurt to test in c:\temp or similar to avoid issues where you may have mistyped your path.
Last, but not least, you need to open in "r" read mode, as previously mentioned.
You should add one more "/" in the last "/" of path for example:
open('C:\Python34\book.csv') to open('C:\Python34\\\book.csv')
Reference
I had this same error appear when trying to read a large file in Python 3.5.4. To solve it, instead of reading the whole file into memory with .read(), I read each line one by one:
with open('big.txt') as f:
for i in f:
print(i)
Just as is written on the Python Documentation, the IOError Exception occurs:
Raised when an I/O operation (such as a print statement, the built-in
open() function or a method of a file object) fails for an I/O-related
reason, e.g., “file not found” or “disk full”.
Open with "r" (read) instead of "w" (write)
And startmenu in these two lines are different?? Try using a forward instead of a back slash. Python will convert the forward slash to the appropriate delimiter for the OS it is running on
open('C:\ProgramData\Microsoft\Windows\Start Menu\Programs\Python 3.4\topheavy.txt','w')
OSError: [Errno 22] Invalid argument:'C:\ProgramData\Microsoft\Windows\Startmenu\Programs\Python 3.4\topheavy.txt'
Replace every \ with \\ in file path
My issue, rather arbitrary, was I was writing a file using open(filename, "w").write(...), where filename is an invalid pathname or includes unexpected slashes.
For example, converting a datetime.datetime.today() into a datestring with slashes or colons (Windows) and writing to non-existing directories will result in this error.
Change:
open("../backup/2021/08/03 15:02:61.json", "w").write(data)
To:
open("../backup/2021-08-03 15-02-61.json", "w").write(backup)
As an example.
I want to get files from directory (which has numbers in directory name). I am using below script. But it is throwing error.
yesterday=140402
os.chdir("C:\pythonPrograms\04-03-2014")
for file in glob.glob("MY*"+str(yesterday)+".log"):
print file
Error received:
WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect: 'C:\\pythonPrograms\x04-03-2014'
Do I need to follow some convention while giving the path? The code works fine if I search in C:\pythonPrograms
"C:\pythonPrograms\04-03-2014"
The issue is the "\04", the \ character is used to denote an escape character, you may know about \n for new line. You can fix this by just doing:
os.chdir(r"C:\pythonPrograms\04-03-2014")
Which makes the string into a raw string. Or you can add another escape character to escape the escape character like:
"C:\\pythonPrograms\\04-03-2014"
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.