R read.csv error: Operation not permittedError in file(file, "rt")

huangapple go评论71阅读模式
英文:

R read.csv error: Operation not permittedError in file(file, "rt")

问题

我在R中使用read.csv()时遇到了无法解决的错误。

我从https://www.finra.org/finra-data/browse-catalog/equity-short-interest/data下载了一个.zip文件(我设置了日期从2019-01-01到2019-12-31,并过滤市场为NYSE;然后我点击了“导出”)。我在我的Macbook上打开了.zip文件,现在在我的下载文件夹中有.csv文件。我将文件重命名为“equityshortinterest_2019.csv”。

现在我执行了以下操作:

Short_Interest <- read.csv("~/Downloads/equityshortinterest_2019.csv")

并且收到了以下错误:

Warning: cannot open file '/Users/macbook/Downloads/equityshortinterest_2019.csv': Operation not permitted
Error in file(file, "rt") : cannot open the connection

我的下载文件夹中的其他.csv文件可以用这种方式打开!我检查了文件的权限,我有读写权限。我还尝试了其他打开csv的方式,但都遇到了相同的错误。

有人知道如何解决吗?先谢谢!

英文:

I have an error with read.csv() in R that I absolutely can not solve.

What I did: I downloaded a .zip file from https://www.finra.org/finra-data/browse-catalog/equity-short-interest/data (I set dates from 2019-01-01 to 2019-12-31 and a filter for Market = NYSE; afterwards I clicked "Export"). I opened the .zip file on my Macbook and now had the .csv file in my downloads folder. I renamed the file to "equityshortinterest_2019.csv".

Now I did

Short_Interest &lt;- read.csv(&quot;~/Downloads/equityshortinterest_2019.csv&quot;)

and got this error

Warning: cannot open file &#39;/Users/macbook/Downloads/equityshortinterest_2019.csv&#39;: Operation not permittedError in file(file, &quot;rt&quot;) : cannot open the connection

Other .csv files in my downloads folder can be opened this way! I checked the permissions for the file and I have read and write permissions. I also tried other ways to open the csv but all gave me the same error.

Does anyone know how to solve this? Thanks in advance!

答案1

得分: 1

以下是您要翻译的内容:

"The issue you are seeing is unlikely related to R (do you have the file open in some other program, for example?), but for more robust workflows you might want to keep external datasets in your RStudio project folder. And if you are not using projects, you probably should, you'd get reliable relative paths.

And/or automate as much as you can, i.e. instead of manually extracting zip archives and renaming resulting files, use something like this instead:

# In Windows, "~" refers to "username/Documents" by default
archive <- "~/../Downloads/equityshortinterest.zip" 
file.exists(archive)
#> [1] TRUE

# unzip(..., list = TRUE) returns dataframe with zip content, 
# [1,1] is Name of the first file
df_ <- unz(archive, unzip(archive, list = TRUE)[1,1]) |> read.csv()

Or just use readr::read_csv() on the archive itself; assuming there's a single CSV in zip, it will handle extraction for you:

readr::read_csv(archive)
#> Rows: 73942 Columns: 11
#> ── Column specification ────────────────────────────────────────────────────────
#> Delimiter: ","
#> chr  (4): Issue Name, Symbol, Market, Revision Flag
#> dbl  (6): Current Short, Previous Short, Chg, % Change from Previous, Avg Da...
#> date (1): Settlement Date
#> 
#> ℹ Use `spec()` to retrieve the full column specification for this data.
#> ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#> # A tibble: 73,942 × 11
#>    `Settlement Date` `Issue Name` Symbol Market `Current Short` `Previous Short`
#>    <date>            <chr>        <chr>  <chr>            <dbl>            <dbl>
#>  1 2019-12-31        3D Systems … DDD    NYSE          23804548         23954237
#>  2 2019-12-31        3M Company   MMM    NYSE           9949341          9957296
#>  3 2019-12-31        500.com Lim… WBAI   NYSE           2509504          2563847
# ...
#> # ℹ 73,932 more rows
#> # ℹ 5 more variables: Chg <dbl>, `% Change from Previous` <dbl>,
#> #   `Avg Daily Vol` <dbl>, `Days to Cover` <dbl>, `Revision Flag` <chr>

For download automation httr / httr2 would suffice:

library(httr2)

# extract cURL request from browser's dev tools and pass it to 
# httr2::curl_translate() for quick and dirty httr2 prototype

dest <- tempfile(fileext = ".zip")
request("https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/export/group/OTCMarket/name/ConsolidatedShortInterest") %>%
  req_headers(`content-type` = "application/json") %>%
  req_body_raw('{"aggregationFilter":null,"arrayFields":[],"compareFilters":[{"fieldName":"marketClassCode","fieldValue":"NYSE","compareType":"EQUAL"}],"dateRangeFilters":[{"fieldName":"settlementDate","startDate":"2019-01-01","endDate":"2019-01-15"}],"delimiter":null,"domainFilters":[],"fileName":"equityshortinterest.csv","fileType":"CSV","groupFields":[],"limit":5000,"mainFields":[{"id":"settlementDate","displayName":"Settlement Date","include":true,"primary":false},{"id":"issueName","displayName":"Issue Name","include":true,"primary":false},{"id":"symbolCode","displayName":"Symbol","include":true,"primary":false},{"id":"marketClassCode","displayName":"Market","include":true,"primary":false},{"id":"currentShortPositionQuantity","displayName":"Current Short","include":true,"primary":false},{"id":"previousShortPositionQuantity","displayName":"Previous Short","include":true,"primary":false},{"id":"changePreviousNumber","displayName":"Chg","include":true,"primary":false},{"id":"changePercent","displayName":"% Change from Previous","include":true,"primary":false},{"id":"averageDailyVolumeQuantity","displayName":"Avg Daily Vol","include":true,"primary":false},{"id":"daysToCoverQuantity","displayName":"Days to Cover","include":true,"primary":false},{"id":"revisionFlag","displayName":"Revision Flag","include":true,"primary":false}],"offset":0,"orFilters":[],"quoteValues":false,"sortFields":["-settlementDate","+issueName"],"singleFile":false,"zipped":true}') %>%
  req_perform(path = dest)
readr::read_csv(dest)

Created on 2023-05-07 with reprex v2.0.2"

英文:

The issue you are seeing is unlikely related to R (do you have the file open in some other program, for example?), but for more robust workflows you might want to keep external datasets in your RStudio project folder. And if you are not using projects, you probably should, you'd get reliable relative paths.

And/or automate as much as you can, i.e. instead of manually extracting zip archives and renaming resulting files, use something like this instead:

# In Windows, &quot;~&quot; refers to &quot;username/Documents&quot; by default
archive &lt;- &quot;~/../Downloads/equityshortinterest.zip&quot; 
file.exists(archive)
#&gt; [1] TRUE

# unzip(..., list = TRUE) returns dataframe with zip content, 
# [1,1] is Name of the first file
df_ &lt;- unz(archive, unzip(archive, list = TRUE)[1,1]) |&gt; read.csv()

Or just use readr::read_csv() on the archive itself; assuming there's a single CSV in zip, it will handle extraction for you:

readr::read_csv(archive)
#&gt; Rows: 73942 Columns: 11
#&gt; ── Column specification ────────────────────────────────────────────────────────
#&gt; Delimiter: &quot;,&quot;
#&gt; chr  (4): Issue Name, Symbol, Market, Revision Flag
#&gt; dbl  (6): Current Short, Previous Short, Chg, % Change from Previous, Avg Da...
#&gt; date (1): Settlement Date
#&gt; 
#&gt; ℹ Use `spec()` to retrieve the full column specification for this data.
#&gt; ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
#&gt; # A tibble: 73,942 &#215; 11
#&gt;    `Settlement Date` `Issue Name` Symbol Market `Current Short` `Previous Short`
#&gt;    &lt;date&gt;            &lt;chr&gt;        &lt;chr&gt;  &lt;chr&gt;            &lt;dbl&gt;            &lt;dbl&gt;
#&gt;  1 2019-12-31        3D Systems … DDD    NYSE          23804548         23954237
#&gt;  2 2019-12-31        3M Company   MMM    NYSE           9949341          9957296
#&gt;  3 2019-12-31        500.com Lim… WBAI   NYSE           2509504          2563847
# ...
#&gt; # ℹ 73,932 more rows
#&gt; # ℹ 5 more variables: Chg &lt;dbl&gt;, `% Change from Previous` &lt;dbl&gt;,
#&gt; #   `Avg Daily Vol` &lt;dbl&gt;, `Days to Cover` &lt;dbl&gt;, `Revision Flag` &lt;chr&gt;

For download automation httr / httr2 would suffice:

library(httr2)

# extract cURL request from browser&#39;s dev tools and pass it to 
# httr2::curl_translate() for quick and dirty httr2 prototype

dest &lt;- tempfile(fileext = &quot;.zip&quot;)
request(&quot;https://services-dynarep.ddwa.finra.org/public/reporting/v2/data/export/group/OTCMarket/name/ConsolidatedShortInterest&quot;) %&gt;% 
  req_headers(`content-type` = &quot;application/json&quot;) %&gt;% 
  req_body_raw(&#39;{&quot;aggregationFilter&quot;:null,&quot;arrayFields&quot;:[],&quot;compareFilters&quot;:[{&quot;fieldName&quot;:&quot;marketClassCode&quot;,&quot;fieldValue&quot;:&quot;NYSE&quot;,&quot;compareType&quot;:&quot;EQUAL&quot;}],&quot;dateRangeFilters&quot;:[{&quot;fieldName&quot;:&quot;settlementDate&quot;,&quot;startDate&quot;:&quot;2019-01-01&quot;,&quot;endDate&quot;:&quot;2019-01-15&quot;}],&quot;delimiter&quot;:null,&quot;domainFilters&quot;:[],&quot;fileName&quot;:&quot;equityshortinterest.csv&quot;,&quot;fileType&quot;:&quot;CSV&quot;,&quot;groupFields&quot;:[],&quot;limit&quot;:5000,&quot;mainFields&quot;:[{&quot;id&quot;:&quot;settlementDate&quot;,&quot;displayName&quot;:&quot;Settlement Date&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;issueName&quot;,&quot;displayName&quot;:&quot;Issue Name&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;symbolCode&quot;,&quot;displayName&quot;:&quot;Symbol&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;marketClassCode&quot;,&quot;displayName&quot;:&quot;Market&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;currentShortPositionQuantity&quot;,&quot;displayName&quot;:&quot;Current Short&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;previousShortPositionQuantity&quot;,&quot;displayName&quot;:&quot;Previous Short&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;changePreviousNumber&quot;,&quot;displayName&quot;:&quot;Chg&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;changePercent&quot;,&quot;displayName&quot;:&quot;% Change from Previous&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;averageDailyVolumeQuantity&quot;,&quot;displayName&quot;:&quot;Avg Daily Vol&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;daysToCoverQuantity&quot;,&quot;displayName&quot;:&quot;Days to Cover&quot;,&quot;include&quot;:true,&quot;primary&quot;:false},{&quot;id&quot;:&quot;revisionFlag&quot;,&quot;displayName&quot;:&quot;Revision Flag&quot;,&quot;include&quot;:true,&quot;primary&quot;:false}],&quot;offset&quot;:0,&quot;orFilters&quot;:[],&quot;quoteValues&quot;:false,&quot;sortFields&quot;:[&quot;-settlementDate&quot;,&quot;+issueName&quot;],&quot;singleFile&quot;:false,&quot;zipped&quot;:true}&#39;) %&gt;%
  req_perform(path = dest)
readr::read_csv(dest)

<sup>Created on 2023-05-07 with reprex v2.0.2</sup>

huangapple
  • 本文由 发表于 2023年5月7日 18:18:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/76193314.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定