简体   繁体   中英

Error with filename

I have a filename declared like this;

filename = Time.now.strftime("%H:%M:%S")+'.json'

and the error occurs when I do this

File.open(filename,'w') do |f|
 f.write(rsp)
end

Error is in `initialize' : Invalid argument - 18:28:20.json which I assume is beacuse of a filename. When I do some 'normal' name everything works OK, so any tips?

尝试:

filename = Time.now.strftime("%H_%M_%S")+'.json'

Windows uses the colon as a drive letter separator; see this SO question for other special chars.

Use a different separator. You might be able to escape it, but IMO, not really worth it.

FWIW, for timestamped filenames I tends towards yyyymmdd-hhmmss or similar anyway.

For things like files it's always good to include more-complete info in the question--that naming conventions are different across OSes is well-known.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM