I read the files in a directory using glob, and then I rename each file to something more legible for my purposes using os.rename.
for file_name in glob.glob(path+'*.txt'):
newfilename = 'run'+str(i)+'.csv' # rename filenames to something more readable
os.rename(file_name,path + newfilename) #put r before path if error ="(unicode error) ‘unicodeescape’ codec can’t decode bytes in position 2-3: truncated \UXXXXXXXX escape"
When I try to write the each new file created into an array (a list) previously intialized to :
filelist=[];
using
filelist.append(i)=newfilename
I get the following error: "SyntaxError: cannot assign to function call"
If I just try to add the file to the filelst array using indeces, ie, filelist[i]=newfilename, I then get an index out of range error.
How do I do create this list of renamed filenames "on the fly"? Thank you.
Ok..so I finally understood that append wants to "append the "thing" to the list, as opposed to "append the thing at index i to the list" like I was trying to do originally.
So the correct way to use append is:
filelist.append(newfilename)
Related
I'm trying to split some datasets in two parts, running a loop over files like this:
cd C:\Users\Macrina\Documents\exports
qui fs *
foreach f in `r(files)' {
use `r(files)'
keep id adv*
save adv_spa*.dta
clear
use `r(files)'
drop adv*
save fin_spa*.dta
}
I don't know whether what is inside the loop is correctly written but the point is that I get the error:
invalid '"e2.dta'
where e2.dta is the second file in the folder. Does this message refer to the loop or maybe what is inside the loop? Where is the mistake?
You want lines like
use "`f'"
not
use `r(files)'
given that fs (installed from SSC, as you should explain) returns r(files) as a list of all the files whereas you want to use each one in turn (not all at once).
The error message was informative: use is puzzled by the second filename it sees (as only one filename makes sense). The other filenames are ignored: use fails as soon as something is evidently wrong.
Incidentally, note that putting "" around filenames remains essential if any includes spaces.
I try to make a new file, according to this question and answer: How to create an empty file by elisp?
I copied the code
(write-region "" nil custom-file)
into a new emacs file and wanted to execute it by C-x C-e. But all i get is
Wrong type argument: stringp, nil
So what am i doing wrong here ? Im pretty new to emacs and have tried finding a solution by googling it but had no success.
Sounds like the value of custom-file is nil. It should be a string (that names a file).
If you set variable debug-on-error to t then you'll get a backtrace that will show you which arg that was expected as a string is actually nil.
Try wrapping your (relative) file name in expand-file-name, like this:
(write-region "" nil (expand-file-name "new_file"))
Now it should create the file in the current default-directory.
It also makes a difference where your cursor is, when executing C-x C-e. Put it at the end of the document.
I'm trying to download documents and strip out the document metadata with the Yomu gem, but cannot find guidance for parsing multiple files. The semi working code is below, and should work if you put some pdf files in the same directory as the script.
require 'yomu'
dir = Dir.pwd
files = Dir["#{dir}/*.pdf"]
def allpdf(files)
filearray = []
files.each do |file|
filearray << file
end
filearray
end
def metadata(dir, allfiles)
array = []
allfiles.each do |file|
yomufile = Yomu.new file
array << yomufile.metadata["Author"]
puts array
end
end
allfiles = allpdf(files)
metadata(dir, allfiles)
So when I 'puts array' it spits out what I would expect. But if I call 'array' outside of the loop, I get a single entry repeated over and over, so I can only assume that the array/yomu hash is being overwritten perhaps. What is the best way to fix this so that I can return a full array for use elsewhere in the application?
Please Note: I suspect this may be a more general Ruby error on my part related to my lack of array skills rather than a Yomu specific issue. Im not sure how else to address this question however.
Jakub Pavlík was correct, the code was actually working as stated, it just wasn't displaying the output in the way I expected!
I write the websocket server via libwebsocket. I need Cookie field to valid user. there is a array named lextable[] for parse http header . I don't know how to modify lextable for Cookie filed.
Ok, go to "lib" directory in your libwebsockets dir. Find the "minilex.c" file, open it with your favorite text editor. At the beginning of the file you will see the "set" string array. You only need to add the line "Cookie: " at the end.
The next step is compiling the program "minilex.c" with command gcc minilex.c -o minilex.
When the compilation is finished, run the binary file and you will see a console consisting of two-dimensional array with the HEX codes of letters and their positions.
Copy-past it into "parsers.c" file.
I wonder how can I update an existing file, and add a word in a given position.
so let say my file looks like:
this the first line in the file
and I want to add the word "is " in position 6 so the file will look like:
this is the first line in the file
what is the best method to achieve that?
what should be the fopen mode?
assume my file is to big to copy to memory, or create a temporary clone
thanks!
There is no magic "insert in the middle" open mode. You have to do that yourself.
If it can't fit in memory, and you don't want/can't create a temporary, you can rewrite it "from the bottom". (I.e. read the last "block", write it back shifted by the amount you want, repeat.)
Unfortunately, it is not possible to simply update a file this way. If it is a flat file, you will have to move the parts yourself.