I want to try using this shutil.copy and my files got some symbolic characters inside its name, so I need to use this shutil.copy(src, dst, *, follow_symlinks=True) command. but the compiler keeps giving me the invalid syntax messages. I had google and didn't find any solution yet for this. anyone can point out what is wrong with my syntax? because I already tried to print out the files inside those directories and it is okay, the network shared folder also got the permission and so on. but don't know what is wrong with my syntax. help me. thanks
This is my current script
and here is the output I got
File "C:\Users\1000266946\Desktop\sa\we.py", line 17
shutil.copy( files, parse_destination_path, * , follow_symlinks = True)
^
SyntaxError: invalid syntax
[Finished in 0.3s]
first of all: the asterisk as you show it can be used in function definitions to define keyword-only arguments (see e.g. here) - but you don't use it in a function call. So in shutil.copy(src, dst, *, follow_symlinks=True) in the docs, it tells you that follow_symlinks can only be used as a keyword argument, not as a positional argument.
second: take a look at the return value of os.listdir(). it returns only file names, without the path as noted in the docs. shutil.copy() however expects a full path, not only the filename. so what you could do is
for file in parse_source_file_list:
shutil.copy(os.path.join(parse_source_path, file), os.path.join(parse_destination_path, file))
further reading: you might also be interested in having a look at glob and pathlib for file handling purposes (docs here and here).
Related
In python, I am reading a file in order to process it.
My goal is to read X lines of that file at once, process the batch of lines and repeat until there is no line left to process in the file.
The StreamReader I am using is of type: <class 'encodings.cp437.StreamReader'>
It is the result of:
with hdfs.read("myfile", encoding="cp437") as reader:
where hdfs is InsecureClient from there
I found this documentation about the cp437 encoded StreamReader readlines.
The documentation says:
sizehint, if given, is ignored since there is no efficient way to
finding the true end-of-line.
What does that actually means ? I do not understand why you would allow a parameter that will be ignored.
What is it even supposed to do ?
According to the documentation it should work as expected, the documentation link you provided is incorrect.
Correct link:https://docs.python.org/3/library/codecs.html#codecs.StreamReader.readlines.
Quoting:
readlines([sizehint[, keepends]])¶ Read all lines
available on the input stream and return them as a list of lines.
Line-endings are implemented using the codec’s decode() method and are included in the
list entries if keepends is true. sizehint,
if given, is passed as the size argument to the stream’s read()
method.
I want to merge two paths using os.path.join() function. Paths I want to merge are- '/Users/Tushar/Desktop/' and '/Exp'.
I was doing - os.path.join('/Users/Tushar/Desktop','/Exp') and
Expected output was - '/Users/Tushar/Desktop/Exp'
But I actually got - '/Exp'
Why am I getting this output?
This kind of output is occurring on all systems, macOS, Windows, Linux
I have tried-
os.path.join('/Users/Tushar/Desktop','Exp') and I got the correct output i.e '/Users/Tushar/Desktop/Exp'
os.path.join('/Users/Tushar/Desktop/','Exp') and I got the correct output again i.e '/Users/Tushar/Desktop/Exp'
os.path.join('/Users/Tushar/Desktop','/Exp','/123') gives '/123' but I expected '/Users/Tushar/Desktop/Exp/123'
Apparently os.path.join('/Users/Tushar/Desktop/,'\\Exp') gives the correct output i.e. '/Users/Tushar/Desktop/\\Exp' where as os.path.join('/Users/Tushar/Desktop/','/Exp') gives the incorrect output '/Exp' .
So far, I have got to the point that it has something to do with the slash (/) at the end of '/Exp' which is the cause of this incorrect output.
From Python documentation
If a component is an absolute path, all previous components are thrown away and joining continues from the absolute path component.
You'll need to manually strip all leading slashes in all components except the first one:
def my_join(root, *args):
args = [arg.lstrip(os.path.sep) for arg in args]
return os.path.join(root, *args)
See example:
>>> my_join('/home/ibug', '/oh', '/yeah', '/handsome')
'/home/ibug/oh/yeah/handsome'
this behavior is exactly as documented
If a component is an absolute path, all previous components are thrown
away and joining continues from the absolute path component.
I want to migrate from python2 to python3.
In code, it opens a file in binary mode like this.
f = open('test', 'rb+')
Because, it needs file seeking like this.
f.seek(-26, 1)
And, the code writes a formatted string to file.
f.writelines("%20s,%04d\n" % (varStr, varInt))
f.writelines('{0:>20s},{1:04d}\n'.format(varStr, varInt))
Upper code is original code and lower code is edited by me.
But, When I run a code, there is an error.
TypeError: a bytes like object is required, not 'int'
Please, help me out from this hell.
There are two mistakes:
You need encode your strings when opening the file in binary mode (to change them in bytes)
writelines must be used with a list instead of a single item (the function has an 's' at the end). In your case, you can use 'write'
Maybe something like this will work:
f.write(("%20s,%04d\n" % (varStr, varInt)).encode())
f.write(('{0:>20s},{1:04d}\n'.format(varStr, varInt)).encode())
I am trying to locate the full name of a file using a wildcard. The code I have is:
MLCNo=crjbis.ffromlot
subfolder=LEFT(mlcno,3)
filename=SYS(2000,'S:\MLC\MLC#\'+subfolder+'xx\'+mlcno+'21391*.pdf')
pathname="S:\MLC\MLC#\"+subfolder+"xx\"+filename
Pathname is passed to a print function to print the file. All of this works great if I don't use a variable in the SYS function (even with a wildcard). I should add that there will only ever be one file returned by the wildcard. Is there another way to do this?
Thanks!!!
Tammy
I'd like to know if it's possible (no matter why) to pass more than just a filename for existing file to stat() function, so it would not fail and return 0?
I mean like this:
struct stat mystat; char file[100];
...
if(stat(file, &mystat)==0){
//success
}
Is it possible to specify file as "existing-file_some_special_chars_maybe_some-text" and to stat() not fail on that?
stat() works on filenames, so if you're passing in something that isn't a filename, you shouldn't be surprised that it fails. You can use fstat() to get information on whatever file a file handle is pointing to, but otherwise you're stuck with just filenames.
How about just creating a list of file names and feeding them to stat() one by one?