I have looked and tried different methods posted online but I cant get this to work.
This is my file path:
file_path = '\\something.com\1400_somethingelse\1400_somethingyes\1400_Design\1500_sketch\ShotCam'
This file path is going to change multiple times so I need this to become a variable.
I have tried using the following in order to convert it into a raw file as another variable:
the r before to get the raw file path and that works but what happens if this file path is going to be change? Naturally I would need a variable
I have used the repr(file_path) and that works in some cases but when I try to use the os.listdir it doesnt!
I have tried the r'%s' %file_path as well but nothing
Im sure its something very simple so please let me know if someone has an answer for this!
Thank you!
**UPDATE
This is the code Im having problems with:
variable = '\\something.com\1400_somethingelse\1400_somethingyes\1400_Design\1500_sketch\ShotCam'
#Result:\something.com`0_somethingelse`0_somethingyes`0_Designh0_sketch\ShotCam
raw_path= repr(variable)
#Result:'\\something.com\x0somethingelse\x0somethingyes\x0Designh\x00_sketch\\ShotCam'
list_dir = os.listdir(raw_path)
print list_dir
#Result: # Error: WindowsError: file <maya console> line 8: 3 #
To make it a raw path I know I have to do:
variable = r'\\something.com\1400_somethingelse\1400_somethingyes\1400_Design\1500_sketch\ShotCam'
But I want this to become a variable so I can change it multiple times.
You can verify that your string is not a valid path by just printing it.
>>> '\\something.com\1400_somethingelse\1400_somethingyes\1400_Design\1500_sketch\ShotCam'
'\\something.com`0_somethingelse`0_somethingyes`0_Designh0_sketch\\ShotCam'
>>> r'\\something.com\1400_somethingelse\1400_somethingyes\1400_Design\1500_sketch\ShotCam'
'\\\\something.com\\1400_somethingelse\\1400_somethingyes\\1400_Design\\1500_sketch\\ShotCam'
use r to escape those \.
Related
I'm having issues when I try to write a file in Python giving it a certain name and not only pathing it. Here is what I'm trying to do:
page_title=page.find('title')
raw_data_path ='output/'+page_title+'_raw.txt'
print (page_title)
with open(raw_data_path, 'w') as file:
file.write ()
When running this code, I receive the error [Errno 22] Invalid argument: 'output/myfile_raw.txt'
If instead of raw_data_path ='output/'+page_title+'_raw.txt' I put, for example, raw_data_path ='output/'+'_raw.txt' the code works well, so for some reason I can't combine the path with the name I'm trying to give the file.
I've searched that error and I see that it is a routing error, so it might be happening because when I want to add the page_title something happens with the path, but I can't see which is the mistake because it should be working.
Can someone give me some help with this issue?
I'm sure this is an easy question but I have searched for a while to no avail. I want to define a file path as a variable and use that variable elsewhere in my python code on Ubuntu. What I have so far is:
filefolder = '/home/Desktop/Sample Loading'
and I call on it later in the code as:
file = open('%f/EventLog.txt' % (filefolder), "a")
When I do this, I get an error saying that a float was expected and not a string. How can I get this to work?
Thanks in advance!
use file = open('%s/EventLog.txt' % (filefolder), "a") with %s instead of %f
But you are much better off using os.path.join(filefolder, 'EventLog.txt')
You passed a formatter that expected a float (%f) not a string (%s).
You can either replace this with %s/Event log.txt or just concatenate it directly like so, filefolder + '/Event log.txt.
Do note that you're better off working with os module directly for essentially anything to do with paths :-)
Will edit answer if you require further explanation as II sent this from phone.
I am using Python 3.3 on Windows 7.
Here is the problem.
When I have a filename starting with a number it changes wrong.
For example:
>>> 'E:\DOCUMENTS\1.jpg'
'E:\\DOCUMENTS\x01.jpg'
I am aware that I can fix it manually by adding an escaping backslash.
>>> 'E:\DOCUMENTS\\1.jpg'
'E:\\DOCUMENTS\\1.jpg'
Or by adding "r" in front of the string.
>>> r'E:\DOCUMENTS\1.jpg'
'E:\\DOCUMENTS\\1.jpg'
But I cannot do it manually, because I don't know what the path will be.
What are the possible solutions?
UPDATE:
As #Blender suggested, I was going to post the code. When I rewrote it, I realized that originally there was a mistake, that leaded me to a wrong conclusion. As far as I have understood, the described above situation, when it is necessary to make a string with a path raw dynamically does not happen. It can only happen when the path is written manually.
import os
from PIL import Image as PIL
from PIL import ImageTk
def searchforimages(dir):
imagelist=[]
for file in os.listdir(dir):
fileabspath=os.path.join(dir,file)
try:
# the problem was here originally, but now it is ok.
# since "fileabspath" get passes as a raw string,
# so there is no problem for PIL.open() to open it
PIL.open(fileabspath)
imagelist.append(fileabspath)
except:
continue
return imagelist
searchforimages('E:\photos')
#the problem only happens, when path is written manually
path='E:\photos\1.jpg'
PIL.open(path)
So now I just want to confirm, the problem when it is necessary to make a string with a path raw dynamically never really happens, does it?
\ only matters when it is used in string literal.
>>> path = input() # `a\n\1` in the following line is typed by me (user).
a\n\1
>>> path
'a\\n\\1'
I have a small problem with reading in my file. My code:
import csv as csv
import numpy
with open("train_data.csv","rb") as training:
csv_file_object = csv.reader(training)
header = csv_file_object.next()
data = []
for row in csv_file_object:
data.append(row)
data = numpy.array(data)
I get the error no such file "train_data.csv", so I know the problem lies with the location. But whenever I specify the pad like this: open("C:\Desktop...etc) it doesn't work either. What am I doing wrong?
If you give the full file path, your script should work. Since it is not, it must be that you have escape characters in your path. To fix this, use a raw-string to specify the file path:
# Put an 'r' at the start of the string to make it a raw-string.
with open(r"C:\path\to\file\train_data.csv","rb") as training:
Raw strings do not process escape characters.
Also, just a technical fact, not giving the full file path causes Python to look for the file in the directory that the script is launched from. If it is not there, an error is thrown.
When you use open() and Windows you need to deal with the backslashes properly.
Option 1.) Use the raw string, this will be the string prefixed with an r.
open(r'C:\Users\Me\Desktop\train_data.csv')
Option 2.) Escape the backslashes
open('C:\\Users\\Me\\Desktop\\train_data.csv')
Option 3.) Use forward slashes
open('C:/Users/Me/Desktop/train_data.csv')
As for finding the file you are using, if you just do open('train_data.csv') it is looking in the directory you are running the python script from. So, if you are running it from C:\Users\Me\Desktop\, your train_data.csv needs to be on the desktop as well.
In my script I want to be able, in the end, to be able to download all files in a directory and all sub-directories... So I am trying FTPLIB. I'm trying to call dir of my ftp server and put it into a variable, but I get NONETYPE?! I can connect to the server and when I call directory = session.dir() It displays a kind of matrix style output in the console with files, read/write perms, dates, etc.... But when I then try to print Directory all I seem to get is "None". My initial idea was to for each item in the directory download them to my computer, but I can't seem to get a list of the directory!
directory = session.dir()
print(str(directory))
Sorry for the long and probably trivial explanation, but I have become a little bit too frustrated.
Any help would be very much appreciated!
-Clem
First, read this. http://docs.python.org/library/ftplib.html#ftplib.FTP.nlst
Then, try this:
directory = session.nlst()
print(directory)
Note.
You don't need to do print(str(...)). The print function gets the string representation for you.
In the official docs, the very first example shows how to do what you need: use .retrlines('LIST') to read the output of LIST command.
Another way is to use .nlst().